Home » excel » eclipse – Solution to NullPointerException in my TestNG test case

eclipse – Solution to NullPointerException in my TestNG test case

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to login Facebook by using multiple sets of login cridential from external excel sheet using TestNG but in between the code what i have written throws:

FAILED: f
java.lang.NullPointerException
    at DataDriven.loginRetesting.f(loginRetesting.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    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.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

Code:

@Test
  public void f() throws Exception{
      FileInputStream fi=new FileInputStream("E:\workspace99\SeleniumAutomations\testdata\LoginData.xls");
      Workbook w=Workbook.getWorkbook(fi);
      Sheet s=w.getSheet(0);
      for (int i = 1; i < 8; i++) {
          driver.findElement(By.id("email")).sendKeys(s.getCell(0,i).getContents());
          driver.findElement(By.id("pass")).sendKeys(s.getCell(1,i).getContents());
          driver.findElement(By.id("u_0_1")).click();
          Thread.sleep(1000);
          if (selenium.isElementPresent("id=userNavigationLabel")) {
              //driver.findElement(By.cssSelector("span.gb_V.gbii")).click();
              driver.findElement(By.id("userNavigationLabel")).click();
              driver.findElement(By.cssSelector("input.uiLinkButtonInput")).click();
              Thread.sleep(1000);
          }
              else{
                  System.out.println("invalid cridential");
                  driver.findElement(By.id("email")).clear();
                  driver.findElement(By.id("pass")).clear();

              }


        }

    }

I am not able to find out where the problem is? so how to solve it.

How to&Answers:

For one thing, don’t use “throws Exception” in the method declaration. Instead, have a try block inside the method with a “not null assertion”.

Keep in mind that you never want to throw typical exceptions in a test method because it will drop you out of the test lifecycle too early and skip your @After phases. An exception thrown in a @Before annotated method will probably cause a SkipAssertion.

So, what you want to do instead is fail assertions. So, if you have a exception condition that occurs sometimes, swallow the exception (don’t throw it) and use an assertion to handle it.

For example:

try {

} catch ( NullPointerException e ) {
    Assert.assertTrue( "There was an error in blah.", false );
}

Answer:

Null pointer means, exepected value of variable is null.

Please check the file exists at the specified path “E:\workspace99\SeleniumAutomations\testdata\LoginData.xls” and you get the value correctly from the file.

Answer:

The stack trace says the issue is line 31 in your code but your snippet does not allow us to work out where line 31 is .

you are calling “selenium.isElementPresent”. Could “selenium” be nil?