0

Could see java.lang.NullPointerException for this code and unable to see the root cause from my side. I appreciate your help.

exception is

java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434) at java.util.Properties.load0(Properties.java:353) at java.util.Properties.load(Properties.java:341) at functions.DriverInitializer.(DriverInitializer.java:22) at testcases.Tc3_FogotPassword.setUp(Tc3_FogotPassword.java:17) 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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

test case class

public class Tc3_FogotPassword {

     static WebDriver webDriver;

     @BeforeClass
        public static void setUp() throws Exception {
            webDriver = DriverInitializer.getDriver(); }

        @Test
        public void navigate() {
            webDriver.get(DriverInitializer.getProperty("login.url"));
            webDriver.manage().window().maximize();
        }

DriverInitializer class

public class DriverInitializer {

    public  static Properties properties = null;
    public static WebDriver driver = null;

     static {
        try {
            properties = new Properties();
        //  properties.load(DriverInitializer.class.getClassLoader()
                    //.getResourceAsStream("application.properties"));

            properties.load(DriverInitializer.class.getClassLoader().getResourceAsStream("application.properties"));

            //System.out.println();

            System.setProperty("webdriver.chrome.driver", properties.getProperty("chrome.path"));  
            System.setProperty("webdriver.gecko.driver", properties.getProperty("gecko.path"));

            switch (getProperty("browser")) {
            case "chrome":
                driver = new ChromeDriver();
                break;
            case "firefox":
                driver = new FirefoxDriver();
                break;
            default:
                driver = new ChromeDriver();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static WebDriver getDriver() {
        return driver;
    }


    public static String getProperty(String key) {
        return properties == null ? null : properties.getProperty(key, "");
    }

properties file

chrome.path=D:/Automation Files/Selenium Traning/Info_Project/drivers/chromedrivers
gecko.path=/Users/rajagopalps/geckodriver
browser=chrome
login.url=https://english.mubasher.info/
lahimadhe
  • 374
  • 2
  • 16
  • 3
    `getResourceAsStream` returns `null` if the resource couldn't be found or the path was incorrect. – QBrute Jan 30 '20 at 11:32
  • you mean "application.properties" file path? if yes where I can change that? – lahimadhe Jan 30 '20 at 11:36
  • 1
    Does this answer your question? [getResourceAsStream returns null](https://stackoverflow.com/questions/16570523/getresourceasstream-returns-null) – QBrute Jan 30 '20 at 11:36
  • I have moved the properties file under the "src" and then the issue was resolved, thanks – lahimadhe Jan 30 '20 at 11:57

0 Answers0