0

I am getting the below ERROR.

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)

My code is :

Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("C:/Users/xxx/Documents/workspace-sts-3.8.3.RELEASE/hello.properties"));

Now ititially it looks like the file is missing or something like that.

So I added a check.

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

This yields are result of :

File exists

So Im all out of options as to what else is missing. The file definitely exists and is properly populated. What else could I be missing ?

  • What you have to do is add `hello.properties` to your class Path then you can load the file like this `this.getClass().getResourceAsStream("hello.properties")` ! – Maraboc May 02 '17 at 15:45
  • 3
    Your call to `getResourceAsStream(...)` returns null, because you misunderstood the meaning of its method argument. See the [javadoc](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String-). It expects a path relative to the classpath, not a path in the file system. – Thomas Fritsch May 02 '17 at 15:47
  • use the below code : properties.load(test.getClass().getResourceAsStream("/config.properties")); /* properties.load(Test.class.getClass().getResourceAsStream("/config.properties")); */ – Lova Chittumuri May 02 '17 at 15:59

1 Answers1

1

Your property file is not in class path, hence It cannot be loaded the way you are trying to load it.

Put the file in classpath e.g. src or resources directory and then try again.

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59