Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在selenium中读取属性文件时发生NullPointerException_Java_Selenium Webdriver_Automated Tests_Properties File_Fileinputstream - Fatal编程技术网

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");
  • 提供完整的属性文件路径 objfile=newfileinputstream(“提供完整属性文件路径”)

  • 如果由于路径错误而未创建对象“objfile”

  • 我们正试图取消对空对象的引用,从而得到空指针异常 prop.load(objfile)


  • 注-我给出的答案可以在评论部分看到。请在接受的同时考虑,感谢

    “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");