java.lang.NullPointerException发生在以下代码中。

huangapple 未分类评论56阅读模式
标题翻译

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(&quot;login.url&quot;));
	        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(&quot;application.properties&quot;));
			
			properties.load(DriverInitializer.class.getClassLoader().getResourceAsStream(&quot;application.properties&quot;));
			
			//System.out.println();
			
			System.setProperty(&quot;webdriver.chrome.driver&quot;, properties.getProperty(&quot;chrome.path&quot;));  
			System.setProperty(&quot;webdriver.gecko.driver&quot;, properties.getProperty(&quot;gecko.path&quot;));

			switch (getProperty(&quot;browser&quot;)) {
			case &quot;chrome&quot;:
				driver = new ChromeDriver();
				break;
			case &quot;firefox&quot;:
				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, &quot;&quot;);
	}

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/

huangapple
  • 本文由 发表于 2020年1月30日 19:26:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/59984966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定