title: Tests are failing with NullPointerException if run through TestNG xml, but work if run independently
body: <p>I have got two tests which I am attempting run through TestNG xml. </p>
  
  <p>If I run those independently, it works. But the first test is failing with a NullPointerException <a href="http://i.stack.imgur.com/1HoYA.png" rel="nofollow">as shown here</a> if I run it as a suite with xml.</p>
  
  <pre><code>package com.Pages;
  
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.support.FindBy;
  import org.openqa.selenium.support.How;
  
  public class HomePage 
  {
  
      WebDriver driver;
  
      @FindBy(how = How.LINK_TEXT, using = ("Log In"))
      WebElement loginlink;
  
      public HomePage(WebDriver driver) {
          this.driver = driver;
      }
  
      public void clicklogin() {
          loginlink.click();
      }
  
  }
  </code></pre>
  
  
  
  <pre><code>package com.Pages;
  
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.support.FindBy;
  import org.openqa.selenium.support.How;
  import org.openqa.selenium.support.ui.ExpectedConditions;
  import org.openqa.selenium.support.ui.WebDriverWait;
  
  public class LoginPage {
  
      WebDriver driver;
  
      @FindBy(how = How.XPATH, using= (".//*[@id='fk-mainbody-id']/div/div/div[1]/div/div[4]/div[2]/input"))
      WebElement email;
  
      @FindBy(how = How.XPATH, using= (".//*[@id='fk-mainbody-id']/div/div/div[1]/div/div[4]/div[4]/input"))
      WebElement pwd;
  
      @FindBy(how = How.XPATH, using= (".//*[@id='fk-mainbody-id']/div/div/div[1]/div/div[4]/div[7]/input"))
      WebElement loginbtn;
  
      @FindBy(how = How.LINK_TEXT, using= ("My Account"))
      WebElement myAccount;
  
      public LoginPage (WebDriver driver) {
          this.driver = driver;
      }
  
      public void enteremail (String Userid) {
          email.sendKeys(Userid);
      }
  
      public  void enterpwd(String passwd) {
          pwd.sendKeys(passwd);
      }
  
      public void clickLoginButton() {
          loginbtn.click();   
      }
  
      public boolean checkMyAccount() {  
          WebDriverWait wait = new WebDriverWait(driver,30);
          wait.until(ExpectedConditions.visibilityOf(myAccount));
          return myAccount.isDisplayed();
      }
  
  }
  </code></pre>
  
  
  
  <pre><code>package com.TestCases;
  
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.testng.annotations.AfterTest;
  import org.testng.annotations.BeforeTest;
  
  public class TestBase {
  
      WebDriver driver;
  
      @BeforeTest
      public void invokeBrowser() {
          driver = new FirefoxDriver();
          driver.get("https://www.flipkart.com");
          driver.manage().window().maximize();    
      }
  
      @AfterTest
      public void teardown() {
          driver.quit();
      }
  
  }
  </code></pre>
  
  
  
  <pre><code>package com.TestCases;
  
  import org.openqa.selenium.support.PageFactory;
  import org.testng.Assert;
  import org.testng.annotations.Test;
  import com.Pages.HomePage;
  
  public class VerifyHomePage extends TestBase 
  {
  
      @Test
      public void VerifyHomePageTitle() throws InterruptedException {
          HomePage hpage = PageFactory.initElements(driver,HomePage.class);
          System.out.println("Test Pass");
          Assert.assertEquals(driver.getTitle(), "Online Shopping India | Buy Mobiles, Electronics, Appliances, Clothing and More Online at Flipkart.com");
          hpage.clicklogin();
      }
  
  }
  </code></pre>
  
  
  
  <pre><code>package com.TestCases;
  
  import org.testng.annotations.Test;
  import org.openqa.selenium.support.PageFactory;
  import org.testng.Assert;
  import com.Pages.HomePage;
  import com.Pages.LoginPage;
  
  public class VerifyLogin extends TestBase {
  
      @Test
      public void checkLoginfunction() throws InterruptedException {
          HomePage hpage = PageFactory.initElements(driver,HomePage.class);
          hpage.clicklogin();
          LoginPage loginp = PageFactory.initElements(driver,LoginPage.class);
          loginp.enteremail("shreshtha.thakre@gmail.com");
          loginp.enterpwd("Shreshtha!1");
          loginp.clickLoginButton();
          loginp.checkMyAccount();
          Assert.assertTrue(loginp.checkMyAccount());
      }
  
  }
  </code></pre>
  
  
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"&gt;
  &lt;suite name="Parallel test suite"&gt;
      &lt;test name="Test"&gt;
          &lt;classes&gt;
              &lt;class name="com.TestCases.VerifyHomePage"/&gt;
              &lt;class name="com.TestCases.VerifyLogin"/&gt; 
          &lt;/classes&gt;
      &lt;/test&gt; &lt;!-- Test --&gt;
  &lt;/suite&gt; &lt;!-- Suite --&gt;
  </code></pre>