Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.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 读取config.properties时出现NULLPOINTER异常_Java - Fatal编程技术网

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的直接值放在