unmuse
unmuse

Reputation: 687

Java NullPointerException on loading properties file

public class SupplierCalculatorApplet extends JApplet{
...
public void init(){
    loadProperties();
    ...
}   

...

private void loadProperties() {
    language = "en-us";//getParameter("Language");
    prop = new Properties();
            try {
                URL urlToProps = this.getClass().getResource("config/" + language + ".properties");
                prop.load(urlToProps.openStream());//Exception Caught Here
            } catch (IOException e) {
            }   
}

Exception is found at the line indicated above. Whether language is a valid properties file or not, I catch the same exception on the same line.

Upvotes: 1

Views: 3995

Answers (2)

unmuse
unmuse

Reputation: 687

Change to:

prop.load(this.getClass().getResourceAsStream("/config/" + language + ".properties")); 

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1075337

You haven't given us a lot to work with there, but my guess would be that urlToProps is null, since Class#getResource returns null if the resource isn't found, but you have no defensive check in the pictured code. So the urlToProps.openStream() part would throw an NPE.

Upvotes: 4

Related Questions