You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium Java加载配置文件时出现空指针异常问题

解决Selenium Java配置文件加载的空指针异常问题

我来帮你排查这个配置文件加载时的空指针问题,先看你代码里的核心错误:

你在拼接配置文件路径的时候写错了System.getProperty的用法!原来的代码里:

private final String propertyFilePath = System.getProperty("user.dir"+"//src//main//resources//configs/Configurations.properties");

这行是把路径直接拼到了"user.dir"这个属性名里,相当于在找一个名为user.dir//src//main//...的系统属性,这肯定找不到,返回的是null。后续用这个null路径创建FileInputStream会抛出异常,导致properties对象根本没被初始化,后续调用配置项的时候自然就触发空指针了。

修正步骤和完整代码

先把路径拼接改成正确的方式,同时用更可靠的方式处理文件流和异常:

package dataProviders;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ConfigFileReader {
    private Properties properties;

    // 正确拼接路径:先获取项目根目录,再用系统分隔符拼接后续路径
    private final String propertyFilePath = System.getProperty("user.dir") 
            + System.getProperty("file.separator") + "src"
            + System.getProperty("file.separator") + "main"
            + System.getProperty("file.separator") + "resources"
            + System.getProperty("file.separator") + "configs"
            + System.getProperty("file.separator") + "Configurations.properties";

    public ConfigFileReader() {
        // 用try-with-resources自动关闭文件流,避免资源泄漏
        try (FileInputStream fis = new FileInputStream(propertyFilePath)) {
            properties = new Properties();
            properties.load(fis);
        } catch (IOException e) {
            System.err.println("加载配置文件失败:" + e.getMessage());
            // 抛出运行时异常,让调用者明确知道配置加载失败,而不是默默吞掉异常
            throw new RuntimeException("配置文件加载失败,请检查路径是否正确", e);
        }
    }

    // 封装获取配置的方法,提前处理空值,避免后续空指针
    public String getProperty(String key) {
        String value = properties.getProperty(key);
        if (value == null) {
            throw new IllegalArgumentException("配置文件中找不到指定的key:" + key);
        }
        return value;
    }
}

更推荐的classpath加载方式(避免路径问题)

如果是Maven/Gradle项目,src/main/resources会被自动加入classpath,直接通过classpath加载配置文件更可靠,不用拼接绝对路径,适配所有环境:

package dataProviders;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigFileReader {
    private Properties properties;

    public ConfigFileReader() {
        // 从classpath加载配置文件
        try (InputStream is = getClass().getClassLoader().getResourceAsStream("configs/Configurations.properties")) {
            if (is == null) {
                throw new IOException("配置文件configs/Configurations.properties在classpath中不存在");
            }
            properties = new Properties();
            properties.load(is);
        } catch (IOException e) {
            System.err.println("加载配置文件失败:" + e.getMessage());
            throw new RuntimeException("配置文件加载失败", e);
        }
    }

    public String getProperty(String key) {
        String value = properties.getProperty(key);
        if (value == null) {
            throw new IllegalArgumentException("配置文件中找不到指定的key:" + key);
        }
        return value;
    }
}

额外排查点

  • 确认你的配置文件确实在src/main/resources/configs/目录下,注意大小写(Linux/macOS路径区分大小写)
  • 如果是Maven项目,确保src/main/resources被标记为资源目录(IDEA中右键目录→Mark Directory as→Resources Root)

内容的提问来源于stack exchange,提问作者Ab123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:00:06