getResourceAsStream的java.lang.NullPointerException

时间:2017-05-02 15:38:17

标签: java

我收到以下错误。

ob_start();
...
...
$output = ob_get_contents();
file_put_contents("cronjob.txt", $output, FILE_APPEND);
ob_flush();

我的代码是:

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");
      }

因此,除了缺少什么之外,我还有很多选择。该文件肯定存在并且已正确填充。我还能错过什么?

1 个答案:

答案 0 :(得分:1)

您的属性文件不在类路径中,因此无法以您尝试加载它的方式加载它。

将文件放在类路径中,例如src或资源目录,然后再试一次。