I am getting java.lang.Null Pointer Exception whenever the program entered to test.log line test base class. And I am not able to debug the code as it is first line of code.
Exception:
[RemoteTestNG] detected TestNG version 7.3.0
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
FAILED CONFIGURATION: @BeforeTest beforeTest(org.testng.TestRunner@5762806e)
java.lang.NullPointerException
at testbase.BaseTest.beforeTest(BaseTest.java:36)
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:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:61)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:366)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:320)
at org.testng.TestRunner.invokeTestConfigurations(TestRunner.java:617)
at org.testng.TestRunner.beforeRun(TestRunner.java:607)
at org.testng.TestRunner.run(TestRunner.java:578)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.runSuites(TestNG.java:1069)
at org.testng.TestNG.run(TestNG.java:1037)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Note: Observed a similar exception in TestNG 7.0 also.
Working with Ubuntu Operating system.
Program :
public Application app;
public SoftAssert assertion;
public String actualvalue;
public String expectedvalue;
public ExtentReports rep;
public ExtentTest test;
@BeforeTest(alwaysRun =true)
public void beforeTest(ITestContext context) throws InterruptedException
{
test.log(Status.INFO, "Welcome to logging"); ------------------------------------------------- Getting error on this line
app = new Application();
// 1 app keyword object for entire test -All @Test
//app.setReport(test);
context.setAttribute("app", app);
app.openBrowser();
app.waiting();
//app.log("Browser Opened");
app.navigateToUploadAndDownload();
assertion= new SoftAssert();
//init and share it with all tests
rep =ExtentManager.getReports();
test =rep.createTest("Upload And Download Settings");
test.log(Status.INFO,"Starting Test"+context.getCurrentXmlTest().getName());
//app.setReport(test);
context.setAttribute("report", rep);
context.setAttribute("test", test);
}
Instructor
Ashish Thakur Replied on 25/11/2021
You have not initialized the test object.
You need to initialize the test object in order to work with it.
So you are getting null pointer exception