Question

I have an Eclipse web project. I have a file called deploy.properties in the root directory which doesn't seem to get read. I have a feeling that I may need to add the file to the "build path" (like a jar), but this is just a guess, when I try to do this there is no option for adding files to the build path so that makes me think I am wrong about that.

Line 89 is this one props.load(stream);

My stack trace looks like this:

java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:365)
at java.util.Properties.load(Properties.java:293)
at sempedia.dao.Dao.getConfigFile(Dao.java:89)
at sempedia.dao.Dao.<clinit>(Dao.java:17) 89 

And the class looks like this:

public class Dao {
private static final String configFileLocation = "/deploy.properties";

private static final Properties configFile = getConfigFile(configFileLocation);

private static final String host = configFile.getProperty("mysql.host");
private static final String port = configFile.getProperty("mysql.port");
private static final String db   = configFile.getProperty("mysql.db");
private static final String user = configFile.getProperty("mysql.user");
private static final String pwd  = configFile.getProperty("mysql.pwd");

public static String getHost() { return host; }

public static String getPort() { return port; }

public static String getDb() { return db; }

public static String getUser() { return user; }

public static String getPwd() { return pwd; }


public static Connection getCon() {
    Connection con = null;
    try {
        String url = "jdbc:mysql://" + host + ":" + port + "/" + db;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con = DriverManager.getConnection(url, user, pwd);

    } catch (SQLException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return con;
}

private static Properties getConfigFile(String fileName) {
    Properties props = new Properties();
    try {
        InputStream stream = Dao.class.getResourceAsStream(fileName);
        props.load(stream);
    } catch (IOException e) {
        System.err.println("Error opening configuration file");
        System.exit(1);
    }

    return props;
}
}
Was it helpful?

Solution

Keep in mind that when you read files in your eclipse project, the default location is in your source directory (if this is a web application, that translates to your classes directory later). So my advice is to try moving the file from the project root directory to your "src" directory and retrying.

OTHER TIPS

Method Dao.class.getResourceAsStream(fileName) returns null as the inputstream if the target file doesn't exist in the classpath - that's why the NullPointerException.

You should either catch it (now you only catch IOException) or test input stream before calling props.load(stream) for null;

Now what root directory do you mean? System root, app source root, app working directory? System root would be a rather bad place to put your config files.

Address it with "deploy.properties" (without the slash at the beginning) and place it in the root of your classpath ("classes", "bin" - or whatever you call it).

If you place it in the default package level of your source directory - either in the source, or a directory that you have added as a source directory, it will be copied to the classpath during compile like this:

/app
  /src
  /config
    deploy.properties 

and now add config directory to the project as a source directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top