从 Selenium 中的配置属性文件读取值时出现空指针异常
Getting Null Pointer Exception While reading value from Config Properties File in Selenium
package com.HybridFramework.testbase;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestBase {
public WebDriver driver;
public Properties a1;
public File f1;
public FileInputStream file;
public void loadPropertiesFile() throws IOException{
Properties a1 = new Properties();
System.out.println(System.getProperty("user.dir"));
//f1= new File("E:\Selenium-2017\HybridFramework\src\main\java\com\HybridFramework\config\config.properties");
f1 = new File(System.getProperty("user.dir")+"/src/main/java/com/HybridFramework/config/config.properties");
System.out.println("lola");
file= new FileInputStream(f1);
a1.load(file);
f1 = new File(System.getProperty("user.dir")+"/src/main/java/com/HybridFramework/config/or.properties");
file= new FileInputStream(f1);
a1.load(file);
}
public void getPropertiesData(){
}
public static void main(String[] args) throws IOException {
TestBase test = new TestBase();
//test.getBrowser("firefox");
System.out.println("Start");
test.loadPropertiesFile();
System.out.println(test.a1.getProperty("Username"));
test.a1.getProperty("testname");
}
}
下面是我正在引用的属性文件。
config.properties 文件
Username=Bhanu
Password=password
url=https://www.google.com
or.properties 文件
test=test1
testname=login
错误低于
线程异常 "main" java.lang.NullPointerException
在 com.HybridFramework.testbase.TestBase.main(TestBase.java:61)
您已声明类型 Properties
的实例 属性 但从未初始化。
在 loadPropertiesFile()
方法中使用 a1 = new Properties();
而不是 Porperties a1 = new Properties();
。
首先使用环境从属性文件中获取 属性。
然后你可以将它设置为系统 属性 然后你可以使用 from System.getProperty()
package com.HybridFramework.testbase;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestBase {
public WebDriver driver;
public Properties a1;
public File f1;
public FileInputStream file;
public void loadPropertiesFile() throws IOException{
Properties a1 = new Properties();
System.out.println(System.getProperty("user.dir"));
//f1= new File("E:\Selenium-2017\HybridFramework\src\main\java\com\HybridFramework\config\config.properties");
f1 = new File(System.getProperty("user.dir")+"/src/main/java/com/HybridFramework/config/config.properties");
System.out.println("lola");
file= new FileInputStream(f1);
a1.load(file);
f1 = new File(System.getProperty("user.dir")+"/src/main/java/com/HybridFramework/config/or.properties");
file= new FileInputStream(f1);
a1.load(file);
}
public void getPropertiesData(){
}
public static void main(String[] args) throws IOException {
TestBase test = new TestBase();
//test.getBrowser("firefox");
System.out.println("Start");
test.loadPropertiesFile();
System.out.println(test.a1.getProperty("Username"));
test.a1.getProperty("testname");
}
}
下面是我正在引用的属性文件。
config.properties 文件
Username=Bhanu
Password=password
url=https://www.google.com
or.properties 文件
test=test1
testname=login
错误低于
线程异常 "main" java.lang.NullPointerException 在 com.HybridFramework.testbase.TestBase.main(TestBase.java:61)
您已声明类型 Properties
的实例 属性 但从未初始化。
在 loadPropertiesFile()
方法中使用 a1 = new Properties();
而不是 Porperties a1 = new Properties();
。
首先使用环境从属性文件中获取 属性。 然后你可以将它设置为系统 属性 然后你可以使用 from System.getProperty()