-2
        public class NewTest 
          {
        @FindBy(id="btnSearch")
        public WebElement search;
        public WebDriver driver;

         @Test
         public void openMyBlog() {
         driver.manage().window().maximize();
         driver.get("url");

           search.click();
             try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        }

      @BeforeClass
      public void beforeClass() {

          System.setProperty("webdriver.chrome.driver", "D:\\Driver\\chromedriver_win32\\chromedriver.exe");
          driver  = new ChromeDriver();
      }

      @AfterClass
      public void afterClass() {
          driver.close();
      }
    }

Received an error as below:

[RemoteTestNG] detected TestNG version 6.14.2
Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) on port 44992
Only local connections are allowed.
Jan 29, 2019 12:59:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
FAILED: openMyBlog
java.lang.NullPointerException
    at testngproject.NewTest.openMyBlog(NewTest.java:28)
    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:580)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    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)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

I tried my best to get rid of the issue, but failed to do so .can any one help me with the resolution to fix this null pointer exception that is received

I tried my best to get rid of the issue, but failed to do so .can any one help me with the resolution to fix this null pointer exception that is received .

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
alwyn sk
  • 1
  • 1
  • 1
    What is the expected output, and on which line does your code fail? – aBnormaLz Jan 29 '19 at 13:53
  • [RemoteTestNG] detected TestNG version 6.14.2 Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) on port 44992 Only local connections are allowed. Jan 29, 2019 12:59:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS FAILED: openMyBlog java.lang.NullPointerException at testngproject.NewTest.openMyBlog(NewTest.java:28) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) – alwyn sk Jan 29 '19 at 13:56
  • Hi @David Pérez Cabrera, this is not a duplicate question for what you have linked. It is related to POM(Page Object Model) design pattern. – Ali Jan 29 '19 at 14:08
  • @AliCSE Not init object and NPE? https://stackoverflow.com/a/218390/4796021 – David Pérez Cabrera Jan 29 '19 at 14:12
  • @@David Pérez Cabrera Oh! Okay, then it's fine. – Ali Jan 29 '19 at 14:14

1 Answers1

1

Add the below code in your program and try to re-run:

NewTest() {
    PageFactory.initElements(driver, this);
}

As you are using POM(Page Object Model), you need to initialize all the elements before using it and PageFactory.initElements() will do that.

Ali
  • 1,689
  • 1
  • 5
  • 12
  • 1
    Could you give us some more detail about your solution? What was the error, how your solution works, etc.? – aBnormaLz Jan 29 '19 at 14:01
  • Refer [This Link](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/PageFactory.html), as you are using the `@FindBy` then you need to use `PageFactory.initElements()` to initialize all the elements fields. So when the class gets invoked, class constructor gets invoked and there by initializing all the elements. – Ali Jan 29 '19 at 14:06