NullPointerException when reading a properties file in Java

前端 未结 11 1340
我在风中等你
我在风中等你 2020-12-09 17:05

I am using the following code to read a properties file:

Properties pro = new Properties();
InputStream is = Thread.currentThread().getContextClassLoader().         


        
11条回答
  •  醉梦人生
    2020-12-09 17:22

    It looks like ClassLoader.getResourceAsStream(String name) returns null, which then causes Properties.load to throw NullPointerException.

    Here's an excerpt from documentation:

    URL getResource(String name): Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

    The name of a resource is a '/'-separated path name that identifies the resource.

    Returns: A URL object for reading the resource, or null if:

    • the resource could not be found, or
    • the invoker doesn't have adequate privileges to get the resource.

    See also

    • Java Tutorials/Loading Images Using getResource
      • Has examples of where/how to put and access resources in the directory/JAR file
    • SDN: Location-Independent Access to Resources

提交回复
热议问题