Reputation: 2065
I've the below code in Java that throws java.lang.NullPointerException
at the line where I create new instance.
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
public static void main(final String... args) {
Properties seleniumProperties = new Properties();
seleniumProperties.setProperty("webdriver.gecko.driver", "<PATH_TO_DRIVER>");
System.setProperties(seleniumProperties);
WebDriver driver = new FirefoxDriver();
}
}
Line no. 14 is WebDriver driver = new FirefoxDriver();
and here's the stack trace of the exception:
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.startsWith(String.java:1405)
at java.lang.String.startsWith(String.java:1434)
at java.util.jar.JarFile.isKnownNotToHaveSpecialAttributes(JarFile.java:594)
at java.util.jar.JarFile.checkForSpecialAttributes(JarFile.java:552)
at java.util.jar.JarFile.hasClassPathAttribute(JarFile.java:518)
at java.util.jar.JavaUtilJarAccessImpl.jarFileHasClassPathAttribute(JavaUtilJarAccessImpl.java:37)
at sun.misc.URLClassPath$JarLoader.getClassPath(URLClassPath.java:1186)
at sun.misc.URLClassPath.getLoader(URLClassPath.java:522)
at sun.misc.URLClassPath.getNextLoader(URLClassPath.java:484)
at sun.misc.URLClassPath.getResource(URLClassPath.java:238)
at java.net.URLClassLoader$1.run(URLClassLoader.java:365)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at Test.main(Test.java:14)
Upvotes: 1
Views: 536
Reputation: 2065
I think I found the problem. Using System.setProperties
probably removes all crucial properties used by JVM. I replaced the code with System.setProperty
as shown below and now it works fine.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
public static void main(final String... args) {
/*
Properties seleniumProperties = new Properties();
seleniumProperties.setProperty("webdriver.gecko.driver", "<PATH_TO_DRIVER>");
System.setProperties(seleniumProperties);
*/
System.setProperty("webdriver.gecko.driver", "<PATH_TO_DRIVER>");
WebDriver driver = new FirefoxDriver();
}
}
Upvotes: 1