Jason Renaldo
Jason Renaldo

Reputation: 2822

Issues with properties file in IntelliJ (adding to classpath) JSP (null pointer ex)

I keep getting a null pointer exception when trying to load my properties file. This is my first time attempting to do this, and in my research it seems I need to add it to the classpath. Here is the code I am currently using to load it in:

    //Set up the properties
    String filePath = "src/prop.properties";
    InputStream inputStream = this.getClass().getResourceAsStream(filePath);

    //Load the properties file
    try
    {
        properties.load(inputStream);
        logger.info("Properties file was loaded successfully.");
    }
    catch(IOException ex)
    {
        logger.severe("Properties file could not be loaded. Exception : " + ex.getMessage());
    }
    catch(NullPointerException ex)
    {
        logger.severe("Null Pointer exception occurred when attempting to load Properties file. Exception : " + ex.getMessage());
    }

To test out a properties file, I just right clicked on the my project and did New -- > File --> prop.properties

enter image description here

And I do not see it here (I may be looking in the complete wrong spot, honestly I am not sure) :

enter image description here

If I am understanding things correctly, shouldn't my prop.properties file be showing up under the src folder? Any help on how to accomplish this would greatly be appreciated, I have looked at other similar posts to no avail.

Thanks.

Upvotes: 0

Views: 1732

Answers (1)

kan
kan

Reputation: 28951

You are placing your properties file into the src (a source code folder), it means the classpath root package. So you should access the file by the getResourceAsStream("/prop.properties").

Upvotes: 4

Related Questions