TestScript run time error NullPointerException



  • I am new to Selenium, when I am working with Page object model in the execution time system displays java.lang.NullPointerException. Here I am going to provide the sample code:

        TestUtil@class
        public class Testbase {
    
    public static WebDriver driver;
    public static Properties prop;
    
    public Testbase()
    {
        try
        {
        prop= new Properties();
        FileInputStream fis = new FileInputStream("E:/TestMaven/DautAutomationTesting/src/main/java/com/qa/config/config.properties");
        prop.load(fis);
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        catch(NullPointerException e)
        {
            e.printStackTrace();
        }
    }
    
    
    public static void initialization()
    {
    String browserName = prop.getProperty("broswer");
    if(browserName.equalsIgnoreCase("chrome"))
    {
    System.setProperty("WebDriver.chrome.driver","C:/Users/lenovo/Downloads/ChromeSetup.exe");
    driver= new ChromeDriver();
    }
    else
    if(browserName.equalsIgnoreCase("firefox"))
    {
    System.setProperty("WebDriver.gecko.driver", "C:/Users/lenovo/Downloads/geckodriver.exe");
    driver= new FirefoxDriver();
    }
    
    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
    driver.get(prop.getProperty("url"));
    
    }
    }
    
    LoginPage@class
        public class Loginpage extends Testbase{
        @FindBy(xpath="//input[@id='email']") WebElement Emailid;
        @FindBy(xpath="//img[@id='enterimg']") WebElement Enter;
        @FindBy(xpath="//img[@id='logo']") WebElement ATlogo;
        
        public Loginpage()
        {
        PageFactory.initElements(driver, this);
            }
        
        public String validatePageTitle()
        {
            return driver.getTitle();
        }
        
        public boolean validateATImage()
        {
            return ATlogo.isDisplayed();
        }
        
        
        public Regpage  Emailid(String eid)
        {
            Emailid.sendKeys(eid);
            Enter.submit();
            return new Regpage();
        }
        }
            
    LoginPageTest@class
    
    public class LoginpageTest extends Testbase{
    
        Loginpage loginpage;
        Regpage regpage;
        
        public LoginpageTest()
        {
            super();
        }
        
        @BeforeMethod
        public void setUp()
        {
            initialization();
            loginpage = new Loginpage();
        }
        
        @Test(priority=1)
        public void loginPageTitleTest()
        {
            String title=loginpage.validatePageTitle();
            Assert.assertEquals(title, "Index");
        }
        
        @Test(priority=2)
        public void  validateATimageTest()
        {
            boolean flag=loginpage.validateATImage();
            Assert.assertTrue(flag);
        }
        }
    
    
    <p><strong>ErrorMessage</strong></p>
    <pre class="lang-java prettyprint-override"><code>java.lang.NullPointerException  at
    com.qa.testbase.Testbase.initialization(Testbase.java:44)   at
    com.qa.test.LoginpageTest.setUp(LoginpageTest.java:24)  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:84)
        at
    org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:653)   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:348)    at
    org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)    at
    org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)     at
    org.testng.SuiteRunner.run(SuiteRunner.java:254)    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.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
    


  • I can see two possible issues here. You need to take the time to debug line by line to leave the only relevant one:

    1. When you call initialization(); the first line is String browserName = prop.getProperty("broswer");. However TestBase constructor is never called explicitly (I am not sure if TestNg does this). This means that your prop object is likely null which causes your code to fail on this line.

    2. If TestNg runs the constructor implicitly using reflection (however this is likely not true) then when you run the line String browserName = prop.getProperty("broswer"); you might be missing property browser in your property file. This means that getProperty() method return null since you do not provide any default value. This eventually causes your code to fail on the line if(browserName.equalsIgnoreCase("chrome"))


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2