title: NullPointerException in Selenium WebDriver Instance When Using in TestNG
body: <p>I have created a Selenium test suite using TestNG for my website.</p>
  
  <p>The name of my project is Test My Website. In order to execute my Selenium test script and create a test report, I execute the TestNG.xml file directly from the command prompt. For different modules of my website, I have created different Java classes for different modules and have kept them in one package. The source code of my files is given as follows:</p>
  
  <p><strong>TestNG.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;suite name="Test My Website"&gt;
      &lt;test name="My Website Test"&gt;
          &lt;packages&gt;
              &lt;package name="testmywebsite" /&gt;
          &lt;/packages&gt;
      &lt;/test&gt;
  &lt;/suite&gt;
  </code></pre>
  
  <p><strong>TestLogin.java</strong></p>
  
  <pre><code>public class TestLogin {
      public static WebDriver driver;
  
      @BeforeTest
      public void setup() {
          driver = new ChromeDriver();
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
          driver.manage().window().maximize();
      }
  
      @Test
      void loginTest() {
          //Code to perform the login test
      }
  
      @AfterClass
      public void setupWorkHistory() {
          TestModule1.driver = driver;
      }
  
  }
  </code></pre>
  
  <p><strong>TestModule1.java</strong></p>
  
  <pre><code>public class TestModule1 {
      public static WebDriver driver;
  
      @Test
      void module1Test() {
          //Code to perform the module 1 test
          driver.getTitle();
      }
  
      @AfterClass
      public void setupModule2() {
          TestModule2.driver = driver;
      }
  }
  </code></pre>
  
  <p><strong>TestModule2.java</strong></p>
  
  <pre><code>public class TestModule2 {
      public static WebDriver driver;
  
      @Test
      void module2Test() {
          //Code to perform the module 2 test
          driver.getTitle();
      }
  
      @AfterTest
      public void tearDown() {
          driver.close();
          driver.quit();
      }
  }
  </code></pre>
  
  <p>Note that in the first two Java classes I have added the <code>setup&lt;Next Class's name&gt;()</code> method to pass my <code>driver</code> instance.</p>
  
  <p>The problem here is that the driver instance gets successfully passed from TestLogin.java to TestModule1.java. However it throws a <code>NullPointerException</code> in the <code>module2Test()</code> method and hence it shows a failed TestNG report for <code>module2Test()</code> despite creating the <code>setupModule2()</code> method in TestModule1.java and adding the <code>@AfterClass</code> annotation to it.</p>
  
  <p>Can anyone tell me why exactly is this happening here? Replies at the earliest will be highly appreciated. Thank you.</p>