1

I've tried variations on the path to the properties file, but can't seem to get it correct.

Here's the structure:

src/
├── properties.properties
└── teln
    ├── ConnectMUD.java
    ├── IOUtil.java
    └── PropertiesReader.java

and the error:

run:
Exception in thread "main" 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 teln.PropertiesReader.getProps(PropertiesReader.java:16)
    at teln.ConnectMUD.main(ConnectMUD.java:18)

and the class in question:

package teln;


import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PropertiesReader {

    private static final Logger LOG = Logger.getLogger(PropertiesReader.class.getName());
    private static Properties props = new Properties();

    public static Properties getProps() {
        try {
            props.load(PropertiesReader.class.getResourceAsStream("/teln/teln.properties"));
        } catch (IOException ex) {
            Logger.getLogger(PropertiesReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        LOG.fine(props.toString());
        return props;
    }
}

The project is Teln and the package is teln (not good choices, maybe).

see also: https://stackoverflow.com/a/8285636/262852

Community
  • 1
  • 1
Thufir
  • 8,216
  • 28
  • 125
  • 273

3 Answers3

5

Your code and setup don't match. You ask for a teln.properties file inside teln but you show a properties.properties file outside of teln. Please be consistent in your question.

The javadoc for Class#getResource(String) says it all.

The ClassLoader will look at the root of the classpath (you can define the classpath in your Netbeans configuration).

With a Netbeans setup like

src/
├── properties.properties
└── teln
    ├── ConnectMUD.java
    ├── IOUtil.java
    └── PropertiesReader.java

we can assume that the classpath root will be

properties.properties
teln/ConnectMud.class
teln/IOUtil.class
teln/PropertiesReader.class

If you want to get the properties.properties resource, you need to get it like so

 props.load(PropertiesReader.class.getResourceAsStream("/properties.properties"));

The prefixed / means to make the path relative to the root of the classpath. If you had ommited it, the method would have looked at the root of the package the class PropertiesReader is in.

Clue: If you are unsure what your classpath is, use Netbeans to compile a Jar of your project and look in there.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • where *should* the properties file be? in the root? – Thufir Aug 28 '13 at 18:31
  • @Thufir It's all up to you. If the properties is only relevant to a certain class, put it in that class' package (and reference it correctly). If the properties file is important for the entire application, it should probably be either at root or in some package containing configuration files. – Sotirios Delimanolis Aug 28 '13 at 18:32
1

Can you try replacing

props.load(PropertiesReader.class.getResourceAsStream("/Teln/teln.properties"));

with

props.load(PropertiesReader.class.getResourceAsStream("/teln/teln.properties"));

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Ankit
  • 1,250
  • 16
  • 23
1

this works:

props.load(PropertiesReader.class.getResourceAsStream("/connection.properties"));

with this structure:

thufir@dur:~/NetBeansProjects/Teln$ 
thufir@dur:~/NetBeansProjects/Teln$ tree src/
src/
├── connection.properties
└── teln
    ├── ConnectMUD.java
    ├── IOUtil.java
    └── PropertiesReader.java

1 directory, 4 files
thufir@dur:~/NetBeansProjects/Teln$ 
Thufir
  • 8,216
  • 28
  • 125
  • 273