-2

I am getting a java.lang.NullPointerException for objUserName.sendKeys(uname);

@FindBy(how=How.XPATH,using="//input[@placeholder='Username']")
static WebElement objUserName;
 public LoginFeature(){
    PageFactory.initElements(config.driver, this);
}

public static String Enterusername(String uname){
    objUserName.sendKeys(uname);
    return uname;
}
public static void main(String[] args ) throws Exception {
    // TODO Auto-generated method stub

    LogF.EnterURL("http://localhost:90/greffa");
    LoginFeature.Enterusername("dummycfo");
    LoginFeature.EnterPwd("passw0rd");
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Learner_me
  • 29
  • 7

2 Answers2

0

PageFactory.initElements inject the webelement object. As you haven't called the constructor method. The object is not initated and it is null object.

Make the page class not static and intiate the class to invoke the constructor method

public LoginFeature {
  @FindBy(how=How.XPATH,using="//input[@placeholder='Username']")
   WebElement objUserName;
   public LoginFeature(){
      PageFactory.initElements(config.driver, this);
  }

  public String Enterusername(String uname){
      objUserName.sendKeys(uname);
      return uname;
  }
}

public Login {
  public static void main(String[] args ) throws Exception {  
      LogF.EnterURL("http://localhost:90/greffa");
      LoginFeature loginFeature= LoginFeature();
      loginFeature.Enterusername("dummycfo");
      loginFeature.EnterPwd("passw0rd");
  }
}
Navarasu
  • 8,209
  • 2
  • 21
  • 32
-1
  1. You need to define and initialize WebDriver instance.
  2. This code:

@FindBy(how=How.XPATH,using="//input[@placeholder='Username']") static WebElement objUserName; public LoginFeature(){ PageFactory.initElements(config.driver, this); }

Better to move to another class as Page object.

  1. After you initialize WebDriver instance, you need to initialize previously created class, using Driver, so you need to refactor this page object.

Only after this part of you code start working.

Ukrainis
  • 534
  • 2
  • 16