getResourceAsStream的java.lang.NullPointerException
我得到下面的错误getResourceAsStream的java.lang.NullPointerException,java,Java,我得到下面的错误 java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434) at java.util.Properties.load0(Properties.java:353) at java.util.Properties.load(Properties.java:341) at org.apache.qpid.example.j
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at org.apache.qpid.example.jmsexample.hello.Hello.runTest(Hello.java:29)
at org.apache.qpid.example.jmsexample.hello.Hello.main(Hello.java:16)
我的代码是:
Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("C:/Users/xxx/Documents/workspace-sts-3.8.3.RELEASE/hello.properties"));
现在它看起来像是文件丢失或类似的东西
所以我加了一张支票
File f = new File("C:/Users/xxx/Documents/workspace-sts-3.8.3.RELEASE/hello.properties");
if(f.exists()) {
System.out.println("File exists");
}
else {
System.out.println("File DOES NOT exists");
}
这是由于:
File exists
所以,我已经没有其他选择了。该文件确实存在并且已正确填充。我还能错过什么 您的属性文件不在类路径中,因此无法按您尝试的方式加载它
将文件放入类路径(例如src或resources目录)中,然后重试。您需要做的是将
hello.properties
添加到类路径中,然后您可以像这样加载文件this.getClass().getResourceAsStream(“hello.properties”)
!对getResourceAsStream(…)
的调用返回null,因为您误解了它的方法参数的含义。看。它需要相对于类路径的路径,而不是文件系统中的路径。请使用以下代码:properties.load(test.getClass().getResourceAsStream(“/config.properties”);/*properties.load(Test.class.getClass().getResourceAsStream(“/config.properties”)*/