标题翻译
java.lang.NullPointerException for below code
问题
这段代码可能会出现 java.lang.NullPointerException 异常,我无法从我的角度看出根本原因。感谢您的帮助。
异常信息如下:
> 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.<clinit>(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)
测试用例类:
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 类:
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"));
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 文件内容:
chrome.path=D:/Automation Files/Selenium Traning/Info_Project/drivers/chromedrivers
gecko.path=/Users/rajagopalps/geckodriver
browser=chrome
login.url=https://english.mubasher.info/
英文翻译
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.<clinit>(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/
专注分享java语言的经验与见解,让所有开发者获益!
评论