title: Null pointer exception when calling a Page Object Model class
body: <p>I am trying my hands on Selenium test automation using TestNG as the test framework. For this, I have used a Page Object pattern to model each of the page of the website that I am writing the test for.</p>
  
  <p>I have two java classes</p>
  
  <ol>
  <li>SignInPage.java (Page Object Model Class)</li>
  <li>TestLogin.java (where the actual test is written and where the SignInPage is instantiated)</li>
  </ol>
  
  <p><strong>SignInPage.java</strong></p>
  
  <pre><code>public class SignInPage {
  private WebDriver driver;
  
  @FindBy(id = "username")
  private WebElement usernameTextbox;
  
  @FindBy(id = "password")
  private  WebElement passwordTextbox;
  
  @FindBy(xpath = "//*[@id=\"left\"]/div[3]/div/form/input[3]")
  private WebElement loginButton;
  
  public SignInPage(WebDriver driver) {
      this.driver = driver;
      driver.get("www.somewebsite.com");
  }
  
  public HomePage performLogin(String username, String password){
      usernameTextbox.sendKeys(username);
      passwordTextbox.sendKeys(password);
      loginButton.click();
      return PageFactory.initElements(driver, HomePage.class);
  }
  }
  </code></pre>
  
  <p><strong>TestLogin.java</strong></p>
  
  <pre><code>public class TestLogin {
  public WebDriver driver;
  
  @BeforeClass
  public void setup(){
      System.setProperty("webdriver.chrome.driver", "path_to_chromedriver.exe");
      driver = new ChromeDriver();
  }
  
  @Test
  public void TestSignIn(){
      SignInPage signInPage = new SignInPage(driver);
      HomePage homePage = signInPage.performLogin("someusername","somepassword");
  }
  }
  </code></pre>
  
  <p>My problem is whenever the signInPage.performLogin() is invoked in the @Test of TestLogin.java, a null pointer exception is encountered.</p>
  
  <pre><code>java.lang.NullPointerException
  at com.tipidpc.webpages.SignInPage.performLogin(SignInPage.java:30)
  at com.tipidpc.tests.TestLogin.TestSignIn(TestLogin.java:26)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
  at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
  at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)
  at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)
  at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
  at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
  at org.testng.TestRunner.privateRun(TestRunner.java:756)
  at org.testng.TestRunner.run(TestRunner.java:610)
  at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
  at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
  at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
  at org.testng.SuiteRunner.run(SuiteRunner.java:289)
  at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
  at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
  at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
  at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
  at org.testng.TestNG.runSuites(TestNG.java:1133)
  at org.testng.TestNG.run(TestNG.java:1104)
  at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
  at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  </code></pre>