【问题标题】:Getting NullPointerException when getting the values from Properties file in Selenium从 Selenium 中的属性文件获取值时获取 NullPointerException
【发布时间】:2023-04-10 12:58:03
【问题描述】:

我试图从属性文件中获取属性(键:值对),但我得到空指针异常。

Properties prop;
@BeforeTest
public void beforeTest() throws IOException {
    prop=new Properties();
    FileInputStream objfile = new FileInputStream(
            "\\resources\\config.properties");
    prop.load(objfile);
    objfile.close();
}

文件夹结构是

【问题讨论】:

  • 我在 com.test.com.test.keywordProperties 文件夹中看到了?
  • 首先,将 .Properties 重命名为 .properties。区分大小写的问题在这里。然后,属性文件不应该在源代码下。它应该在资源文件夹下。创建资源文件夹并将 Config.properties 文件放入其中。你不应该像这样给出绝对路径。您必须使用相对路径,因为文件在您的项目中
  • 我将 .Properties 更新为 .properties。创建资源文件夹并将 config.properties 转移到该资源文件夹我仍然收到 NullPointerException。

标签: java selenium selenium-webdriver properties


【解决方案1】:

您应该将属性作为资源加载,因为它们是您项目的一部分。无需使用完整的文件系统路径。

Properties prop = new Properties();

@BeforeTest
public void beforeTest() throws Exception {
  try (InputStream in = getClass().getResourceAsStream("/Config.properties") {
    prop.load(in);
  }  
}

请注意,不建议使用默认 Java 包以避免类路径冲突。将Config.properties 移动到一个命名的包将解决它。

【讨论】:

  • 我更新了相对路径的路径并将代码更新为您建议的代码我仍然收到 NullPointerException
  • 为此,您应该将资源文件夹放入 Java 构建路径中。
  • 我已经更新了我的代码和截图,请您检查并告诉我
【解决方案2】:

您是否尝试过在调试模式下运行?好像你的道具对象是空的。所以没有什么可以加载的。您需要在方法中创建 Properties 对象。将构造函数移到方法内部,然后检查。

@BeforeTest
    public void beforeTest() throws IOException {
        Properties prop=new Properties();
        FileInputStream objfile = new FileInputStream(
                "C:\\Users\\psailaja\\workspace\\TestPropertiesAndKeyword\\Config.properties");
        prop.load(objfile);
        objfile.close();
    }

【讨论】:

  • 即使我得到 nullPointerException,我也将 Properties obj 推送到 Method 中
  • 请查看我更新的答案。请记下我使用的绝对路径。您需要使用正确的绝对路径。
  • @sailaja.p 你能发布 NullPointer 错误跟踪的完整日志吗?我想知道究竟是什么抛出了 NullPointer。属性文件里面还有什么?它是空的吗?
【解决方案3】:

在尝试从 属性文件中获取 properties(key:value pairs) 时,您会遇到 空指针异常。您必须注意以下几个事实:

  • 将属性文件的名称从config.properties更改为config.property
  • 在您获得项目位置的帮助时,请通过 . 使用参考
  • 需要初始化Properties类型的对象prop
  • 这是您的工作代码:

    Properties prop;
    @BeforeTest
    public void beforeTest() throws IOException {
    
        File src = new File("./resources/config.property");
        FileInputStream fis = new FileInputStream(src);
        prop = new Properties();
        prop.load(fis);
        String propValue = prop.getProperty("propKey");
    }
    

【讨论】:

  • 我已经更新了 Properties 属性的代码; @BeforeTest public void beforeTest() throws IOException { File src=new File("./resources/config.property"); FileInputStream objfile = new FileInputStream(src);道具=新属性(); prop.load(objfile); objfile.close(); DriverPath = prop.getProperty("driverPath");仍然我得到空指针异常
  • @sailaja.p 有什么急事objfile.close()
【解决方案4】:

感谢回复

问题是由于没有为从属性文件中提取的变量声明有效的访问修饰符(公共静态)。 以下几点没有问题

  • 我们可以在不使用资源文件夹的情况下声明属性文件
  • 我们可以将属性文件声明为 config.properties 或 config.property,不需要任何专有名称
  • 我们可以不使用格式getClass().getResourceAsStream("/Config.properties")通过FIS获取文件

【讨论】:

    猜你喜欢
    • 2013-02-08
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 2013-05-07
    相关资源
    最近更新 更多