NullPointerException when reading a properties file in Java

前端 未结 11 1342
我在风中等你
我在风中等你 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
    0 讨论(0)
  • 2020-12-09 17:22

    I had the same problem and was quite confused as I used it previously in a Sturts application. But the problem was that I didn't understand the type of ClassLoader that Struts returns is different than what Spring returns. And the way i figured it out was i printed out the object that was returned on to the system console like this:

    System.out.println(Thread.currentThread().getContextClassLoader());
    

    [
    WebappClassLoader
    context: /MyProject
    delegate: false
    repositories:
    /WEB-INF/classes/
    ----------> Parent Classloader:
    org.apache.catalina.loader.StandardClassLoader@1004901
    ]

    It gave me the detail of the object, and in that I found its type to be of WebAppClassLoader which will start looking for files in the WEB-INF/classes/ folder after a build is done. So I went into the that folder and looked for where my file is located so I gave the path accordingly.

    In my case it was located in /WEB-INF/classes/META-INF/spring/filename.extension

    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("META-INF/spring/filename.extension");
    

    Voilà!

    That fixed it all.

    0 讨论(0)
  • 2020-12-09 17:27

    Bugfixing is easier if you write more lines, like:

    Properties properties = new Properties();
    Thread currentThread = Thread.currentThread();
    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
    InputStream propertiesStream = contextClassLoader.getResourceAsStream("resource.properties");
    if (propertiesStream != null) {
      properties.load(propertiesStream);
      // TODO close the stream
    } else {
      // Properties file not found!
    }
    
    0 讨论(0)
  • 2020-12-09 17:30

    In Case its in eclipse clean the project and in case it is intellij rebuild the project it should start working

    0 讨论(0)
  • 2020-12-09 17:37

    Well it depends; As per javadoc ... The context ClassLoader is provided by the creator of the thread for use by code running in this thread when loading classes and resources. If not set, the default is the ClassLoader context of the parent Thread. The context ClassLoader of the primordial thread is typically set to the class loader used to load the application...

    So if Thread.currentThread().getContextClassLoader() is in the main() function and you haven't created any thread then it should have the same package as that of the class containing method main. Otherwise it should be present in the class which has created the thread....

    0 讨论(0)
提交回复
热议问题