title: Getting NullPointerException if I run tests through testng.xml, but it works fine one at a time
body: <p><strong>Prologue:</strong> I am new to automation testing</p>
  
  <p>I have problem with two test, which I try to run through testng.xml as a suite. But if I ran them <strong>separately</strong>, they would work fine.
  Here is my code:
  <strong>BasePage.java</strong></p>
  
  <pre><code>public class BasePage {
      protected WebDriver driver;
      public BasePage(WebDriver driver) {
          this.driver = driver;
      }
  }
  </code></pre>
  
  <p><strong>BaseTest.java</strong></p>
  
  <pre><code>public class BaseTest {
      private WebDriver driver;
      private final String chromeDriverPath = "src//main//resources//chromedriver.exe";
      private final String firefoxDriverPath = "src//main//resources//geckodriver.exe";
  
      public WebDriver getDriver() {
          return driver;
      }
  
      @BeforeTest
      @Parameters({"browser", "baseURL"})
      public void setUp(@Optional String browser, @Optional String baseURL) {
          driver = getBrowserType(browser);
          driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
          driver.manage().window().maximize();
          driver.navigate().to(baseURL);
      }
  
      private WebDriver getBrowserType(String browserType) {
          if (driver == null ) {
              if (browserType.equalsIgnoreCase("firefox")) {
                   System.setProperty("webdriver.gecko.driver", firefoxDriverPath);
                   return  new FirefoxDriver();
              }
              else if (browserType.equalsIgnoreCase("chrome")) {
                  System.setProperty("webdriver.chrome.driver", chromeDriverPath);
                  return new ChromeDriver();
              }
          }
          return driver;
      }
  
      @AfterTest
      public void tearDown() {
          if (driver != null)
              driver.quit();
      }
  }
  </code></pre>
  
  <p><strong>HomePage.java</strong></p>
  
  <pre><code>public class HomePage extends BasePage {
  
      public HomePage(WebDriver driver) {
          super(driver);
      }
  
      public boolean verifyHomePage() {
          return driver.getCurrentUrl().contains("url");
      }
  }
  </code></pre>
  
  <p><strong>NavigationMenuComponent.java</strong></p>
  
  <pre><code>public class NavigationMenuComponent extends BasePage{
  
      public NavigationMenuComponent(WebDriver driver) {
          super(driver);
      }
  
      @FindBy(xpath = "//ul[@class='right']//a[contains(@href, '/about')]")
      private WebElement aboutLink;
  
      public void test() {
           aboutLink.click();
      }
  }
  </code></pre>
  
  <p><strong>HomePageTest.java</strong></p>
  
  <pre><code>public class HomePageTest extends BaseTest {
  
      private HomePage homePage;
  
      @BeforeClass(alwaysRun = true)
      public void init() {
          homePage = PageFactory.initElements(getDriver(), HomePage.class);
      }
  
      @Test(groups = "home-page")
      public void verifyFirstPage() {
          Assert.assertTrue(homePage.verifyHomePage());
      }
      }
  </code></pre>
  
  <p><strong>NavigationMenuTest.java</strong></p>
  
  <pre><code>public class NavigationMenuTest extends BaseTest{
  
      private NavigationMenuComponent navigationMenuComponent;
  
      @BeforeClass
      public void init() {
          navigationMenuComponent = PageFactory.initElements(getDriver(), NavigationMenuComponent.class);
      }
  
      @Test(groups = "about-page")
      public void verifyAboutLink() {
         navigationMenuComponent.test();
      }
  }
  </code></pre>
  
  <p>And my <strong>testng.xml</strong></p>
  
  <pre><code>&lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"&gt;
  &lt;suite name="suite" parallel="tests" thread-count="2"&gt;
      &lt;parameter name="baseURL" value="https://www.foodpanda.com/"&gt;&lt;/parameter&gt;
      &lt;test name="chromeTest" &gt;
          &lt;parameter name="browser" value="chrome"&gt;&lt;/parameter&gt;
          &lt;classes&gt;
              &lt;class name="tests.HomePageTest"&gt;&lt;/class&gt;
              &lt;class name="tests.NavigationMenuTest"&gt;&lt;/class&gt;
          &lt;/classes&gt;
      &lt;/test&gt;
      &lt;test name="firefoxTest"&gt;
          &lt;parameter name="browser" value="firefox"&gt;&lt;/parameter&gt;
          &lt;classes&gt;
              &lt;class name="tests.HomePageTest"&gt;&lt;/class&gt;
              &lt;class name="tests.NavigationMenuTest"&gt;&lt;/class&gt;
          &lt;/classes&gt;
      &lt;/test&gt;
  &lt;/suite&gt;
  </code></pre>
  
  <p>Here is stacktrace</p>
  
  <pre><code>java.lang.NullPointerException
      at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
      at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
      at com.sun.proxy.$Proxy9.click(Unknown Source)
      at utils.NavigationMenuComponent.test(NavigationMenuComponent.java:36)
      at tests.NavigationMenuTest.verifyAboutLink(NavigationMenuTest.java:25)
      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:497)
      at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
      at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
      at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
      at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
      at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
      at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
      at org.testng.TestRunner.privateRun(TestRunner.java:767)
      at org.testng.TestRunner.run(TestRunner.java:617)
      at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
      at org.testng.SuiteRunner.access$000(SuiteRunner.java:37)
      at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
      at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>I am stuck on this one. Should i run them seperately or i am doing smh wrong?</p>