Skip to content
Advertisement

i am getting a java.lang.NullPointerException while executing the testng script

while running the code i am getting this java.lang.NullPointerException in chrome, i have extended the main login class to another and its showing this exception. i have extended the same main class to another diff class and its successfully running but only this class is showing error.

here is the code

//login page public class admin_login {

//public static void main(String args[]) throws InterruptedException{
WebDriver driver;
@Test
public void login() throws InterruptedException{    
    System.setProperty("webdriver.chrome.driver","E:\Automation\jarfiles2\chromedriver_win32_new\chromedriver.exe");
    driver = new ChromeDriver();
    
    driver.get("http://localhost/Login");
    driver.manage().window().maximize();
    Thread.sleep(2000);
    //driver.close();

    driver.findElement(By.id("UserName")).sendKeys("admin");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    driver.findElement(By.id("Password")).sendKeys("adminadmin");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    driver.findElement(By.cssSelector("input.btn")).click();
}

}

//Userclass

public class User extends admin_login{

@Test
public void user() throws InterruptedException{
 //master dropdown  
//driver.findElement(By.xpath("//*[@id='topNavbarCollapse']/ul[1]/li[1]/a")).click();
    try{
        driver.findElement(By.xpath("//*[@id='topNavbarCollapse']/ul[1]/li[1]/a")).click();
        
    }
    catch(java.lang.NullPointerException n){
        //System.out.println("exception handaled");
        System.out.println(n.getMessage());
    }
    finally{
        driver.findElement(By.xpath("//*[@id='topNavbarCollapse']/ul[1]/li[1]/a")).click();
    }

i have tried to get normally and then via try catch also but its still showing the same exception and test failed

stack trace

java.lang.NullPointerException
    at Finlogin.User.user(User.java:19)
    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)

Advertisement

Answer

  • 1 java.lang.NullPointerException appeared because the driver was null.

    You’ve decided to initialize the driver in login() test-method. So user() will work only when login() method has been executed before (and the driver was initialized).

  • 2 When you run some Test class that is extended from another Test class, tests will be invoked for both classes. But the order might be different. At least I’ve found different behavior when the class name started from A and the class name started from Z. So that might be the reason why it works for some other class (with another name).

Solution

I suggesting:

Initialize common test resources in @Before configuration methods.

e.g.

@BeforeClass
public void initDriver() {
    System.setProperty("webdriver.chrome.driver", "E:\Automation\jarfiles2\chromedriver_win32_new\chromedriver.exe");
    driver = new ChromeDriver();
}

@Test
public void login() throws InterruptedException{    
    driver.get("http://localhost/Login");
...

@AfterClass
public void closeDriver() {
    driver.quit();
}

So you’ll avoid the test-methods execution order dependencies.

Advertisement