0

I have written the below code which is one of the programs in designing a framework and it is giving me this below error every time. Can someone pls help me in fixing this issue. [RemoteTestNG] detected TestNG version 7.2.0 FAILED: basePageNavigation java.lang.NullPointerException

package ArjunSelenium.E2Eproject;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;

public class Base {
    
    public WebDriver driver=null;
    
    @Test
    public WebDriver initializeDriver() throws IOException {
        
        Properties prop=new Properties();
        FileInputStream fis=new FileInputStream("C:\\Users\\Arjun\\eclipse-workspace\\E2Eproject\\src\\main\\java\\ArjunSelenium\\E2Eproject\\data.properties");

        prop.load(fis);
        
        String browserName=prop.getProperty("browser");
        
        if(browserName.equals("chrome")) {
            
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\Arjun\\Desktop\\ChromeDriver\\chromedriver.exe");
            driver = new ChromeDriver();
            
        }else if(browserName.equals("firefox")) {
            System.setProperty("webdriver.gecko.driver", "C:\\Users\\Arjun\\Desktop\\firefoxdriver\\geckodriver.exe");
            driver = new FirefoxDriver();
            
        }else if(browserName.equals("IE")) {
            System.setProperty("webdriver.ie.driver", "C:\\Users\\Arjun\\Desktop\\ied2\\IEDriverServer.exe");
            driver = new InternetExplorerDriver();
            
        }
         
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return driver;
    }
}

and below is the test base class which the above program is extending

package ArjunSelenium.E2Eproject;

import java.io.IOException;

import org.testng.annotations.Test;

public class HomePage extends Base{
    
    @Test
    public void basePageNavigation() throws IOException {
        
        driver=initializeDriver();
        driver.get("https://www.youtube.com/");
      }
}

AND THIS IS POPPING UP IN MY CONSOLE

[RemoteTestNG] detected TestNG version 7.2.0
FAILED: basePageNavigation
java.lang.NullPointerException
    at ArjunSelenium.E2Eproject.Base.initializeDriver(Base.java:43)
    at ArjunSelenium.E2Eproject.HomePage.basePageNavigation(HomePage.java:12)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:821)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:588)
    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:1214)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1136)
    at org.testng.TestNG.runSuites(TestNG.java:1066)
    at org.testng.TestNG.run(TestNG.java:1034)
    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)


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


===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0`enter code here`
===============================================

PLEASE HELP ME OUT TO REMOVE THIS ERROR.

Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26
Arjun
  • 1
  • 1
  • 1
  • browserName may be empty . So driver is still null as its not going inside if else condition . Add one default condition if no browserName matches use chrome or on something on similar lines – Rahul L Jul 14 '20 at 12:31
  • Yes Rahul, that was the error, thank you! solved it. – Arjun Jul 14 '20 at 12:35

0 Answers0