Java 读取config.properties时出现NULLPOINTER异常
读取Java 读取config.properties时出现NULLPOINTER异常,java,Java,读取config.properties时未找到文件config.properties就在com/wu/resources下面。可能是什么问题 Properties prop = new Properties(); InputStream input; input = Readproperties.class.getClassLoader().getResourceAsStream("/com/wu/resources/config.properties"); if (input != null
config.properties
时未找到文件config.properties
就在com/wu/resources
下面。可能是什么问题
Properties prop = new Properties();
InputStream input;
input = Readproperties.class.getClassLoader().getResourceAsStream("/com/wu/resources/config.properties");
if (input != null) {
prop.load(input); }
else {
System.out.println("file not found");
}
它似乎返回null,这将导致Properties.load抛出NullPointerException
:查找具有给定名称的资源。资源是一些数据(图像、音频、文本等),它们可以由类代码以独立于代码位置的方式访问。
资源的名称是一个以“/”分隔的路径名,用于标识资源
返回:用于读取资源的URL对象,或null如果:
- 找不到资源,或
- 调用程序没有足够的权限获取资源
input = Readproperties.class.getClassLoader().getResourceAsStream("src/com/wu/resources/config.properties");
尝试输入=Readproperties.class.getClassLoader().getResourceAsStream(“config.properties”);删除前导的
/
-您正在调用ClassLoader.getResourceAsStream()
,而不是Class.getResourceAsStream()
@wero-/com/wu/resources在src下…是否有办法在此处附加屏幕截图?@Nick-Read.properties和config.properties在不同的包中,因此,将config.properties的直接值放在