Java 在selenium中读取属性文件时发生NullPointerException
我有以下代码:Java 在selenium中读取属性文件时发生NullPointerException,java,selenium-webdriver,automated-tests,properties-file,fileinputstream,Java,Selenium Webdriver,Automated Tests,Properties File,Fileinputstream,我有以下代码: public class BasePage { public static WebDriver driver; public static Properties prop; FileInputStream objfile; @Test public void BasePages() throws IOException { try { prop = new Properties();
public class BasePage {
public static WebDriver driver;
public static Properties prop;
FileInputStream objfile;
@Test
public void BasePages() throws IOException {
try {
prop = new Properties();
objfile = new FileInputStream(System.getProperty("app.properties"));
prop.load(objfile);
System.out.println("file loaded");
} catch (Exception e) {
System.out.println("catch exception" + e);
}
}
}
FileInputStream
显示NullPointerException
。我只是在阅读了这一行代码并将其移动到catch块之后才尝试调试上面的代码
有人能解释我为什么会遇到异常以及如何解决这个问题吗
app.properties
文件包含以下行:
baseUrl = "https://www.google.com/";
browser="chrome";
请尝试以下解决方案:
try {
InputStream objfile = new FileInputStream("path/to/app.properties")) {
Properties prop = new Properties();
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("baseUrl"));
System.out.println(prop.getProperty("browser"));
} catch (IOException ex) {
ex.printStackTrace();
}
上面的行导致了问题,因为System.getProperty(“app.properties”)
返回null。不知道你为什么要用这个
只需删除System.getProperty()
,如下所示。确保您使用的文件路径正确
objfile = new FileInputStream("app.properties");
若您打算访问当前项目目录,那个么代码应该是
objfile = new FileInputStream(System.getProperty("user.dir")+"/"+"app.properties");
注-我给出的答案可以在评论部分看到。请在接受的同时考虑,感谢“public无效初始化”{StrasBaseLL= PROP.GETFACTS(“BaseURL”);StryBrowser= PROP.GETFACTS(“浏览器”);Soal.Out.PrtLn(BaseLL);Soal.Out.PrtLn(浏览器);如果(Browser.CopeNetrQuales(“Chrome”)){Stest.Stand属性()“webdriver.chrome.driver”、“C:\\Windows\\chromedriver.exe”);driver=new chromedriver();System.out.println(“chrome加载”);}System.out.println(baseUrl);System.out.println(浏览器);driver.get(baseUrl);}``为什么要初始化?”objfile“2次在类级别和内部测试函数中??这是一个键入错误我编辑了代码@Amit jainobjfile=new FileInputStream(“给出完整属性文件路径”)@Madhu,如果有任何答案对您有帮助,请单击计票下面的勾号来接受。因此,它可以帮助其他人。谢谢
objfile = new FileInputStream(System.getProperty("user.dir")+"/"+"app.properties");