title: How to resolve java.lang.NullPointerException in Page Object Model in Selenium?
body: <p><strong>TestBase Class:-</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class TestBase {
      public static WebDriver driver;
      public static Properties prop;
  
      // TestBase class constructor is used to initialize the properties object to
      // fetch config variables from config.properties file.
      public TestBase() {
  
          try {
  
              // File src = new File("./src/main/java/com/config/config.properties");
              File src = new File(".\\src\\main\\java\\com\\config\\config.properties");
              FileInputStream fs = new FileInputStream(src);
              prop = new Properties();
              prop.load(fs);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public static void initialization() throws InterruptedException {
          String browserName = prop.getProperty("Browser");
          if (browserName.equals("chrome")) {
              System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver_win32\\chromedriver.exe");
              driver = new ChromeDriver();
          } else {
              System.out.println("Oops! Exception has Caught");
          }
  
          driver.manage().window().maximize();
          driver.get(prop.getProperty("URL"));
      }
  }
  </code></pre>
  
  <p><strong>HomePage Class:-</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class HomePage extends TestBase {
  
      TestUtil testUtil;
  
      @FindBy(xpath = "//a[@title='Update your profile, personal settings, and more']")
      WebElement updateProfile;
  
  
      @FindBy(xpath = "//a[@title='Create a New Post']")
      WebElement createNewPost;
  
      @FindBy(xpath="(//span[@class='count'])[2]")
      WebElement drafts;
  
      @FindBy(xpath="//a[@class='button masterbar__recent-drafts-see-all is-compact is-borderless']")
      WebElement seeAll;
  
      public HomePage() {
          PageFactory.initElements(driver, this);
      }
  
      public ProfilePage updateYourProfile() throws AWTException, InterruptedException {
          updateProfile.click();
          Thread.sleep(3000);
          return new ProfilePage();
      }
  
      public NewPostPage createNewPost() throws InterruptedException {
          createNewPost.click();
          Thread.sleep(3000);
          return new NewPostPage();
      }
  
      public DraftsPage draftsPage() throws InterruptedException {
          createNewPost.click();
          Thread.sleep(3000);
          drafts.click();
          Thread.sleep(3000);
          seeAll.click();
          Thread.sleep(3000);
          return new DraftsPage();
      }
  }
  </code></pre>
  
  <p><strong>DraftsPage Class:-</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class DraftsPage extends TestBase {
  
      @FindBy(xpath="(//button[@title='Toggle menu'])[1]")
      WebElement toggleMenu;
  
      @FindBy(xpath="(//button[@role='menuitem'])[2]")
      WebElement trash;
  
      public void toggleMenu() throws InterruptedException {
          toggleMenu.click();
          Thread.sleep(3000);
          trash.click();
          Thread.sleep(3000);
      }
  }
  </code></pre>
  
  <p><strong>DraftsPageTest Class:-</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>public class DraftsPageTest extends TestBase {
  
      ProfilePage myProfile;
      LoginPage loginpage;
      HomePage homepage;
      NewPostPage newPostPage;
      DraftsPage draftspage;
  
      public DraftsPageTest() {
          super();
      }
  
      @BeforeMethod
      public void setUp() throws InterruptedException {
          initialization();
          loginpage = new LoginPage();
          loginpage.login();
  
          Thread.sleep(3000);
          loginpage.userName(prop.getProperty("username"));
          Thread.sleep(3000);
          homepage = loginpage.password(prop.getProperty("password"));
          Thread.sleep(3000);
          myProfile = new ProfilePage();
          Thread.sleep(3000);
          newPostPage = new NewPostPage();
          Thread.sleep(3000);
          draftspage = new DraftsPage();
          Thread.sleep(3000);
      }
  
      @Test
      public void DraftsPageTest() throws InterruptedException {
          homepage.draftsPage();
          Thread.sleep(3000);
          draftspage.toggleMenu();
          Thread.sleep(3000);
      }
  
      @AfterMethod
      public void close() {
          driver.close();
      }
  }
  </code></pre>
  
  <p><strong>Error Message:-</strong></p>
  
  <pre><code>FAILED: DraftsPageTest
  java.lang.NullPointerException
      at com.pages.DraftsPage.toggleMenu(DraftsPage.java:18)
      at com.testCases.DraftsPageTest.DraftsPageTest(DraftsPageTest.java:51)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
      at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
      at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
      at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
      at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
      at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
      at org.testng.TestRunner.privateRun(TestRunner.java:648)
      at org.testng.TestRunner.run(TestRunner.java:505)
      at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
      at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
      at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
      at org.testng.SuiteRunner.run(SuiteRunner.java:364)
      at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
      at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
      at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
      at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
      at org.testng.TestNG.runSuites(TestNG.java:1049)
      at org.testng.TestNG.run(TestNG.java:1017)
      at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
      at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
      at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
  </code></pre>
  
  <p><strong>Description:-</strong></p>
  
  <p>I am trying to develop TestNG Framework with the help of Selenium. 
  I have developed HomePage, LoginPage, ProfilePage, NewPostPage, DraftsPage. While I was running the <code>DraftsPageTest.java</code>, <strong>@BeforeMethod</strong> was working fine 
  and the moment control comes to <strong>@Test</strong>, <code>homepage.draftsPage();</code> is also working fine. But the moment control comes to <code>draftspage.toggleMenu();</code> it is throwing </p>
  
  <blockquote>
    <p>java.lang.NullPointerException.</p>
  </blockquote>
  
  <p>Any help will be highly appreciated.</p>
  
  <p>Thanks in advance:)</p>