Reputation:
In a program I'm working on I have
String cwd;
String file_separator;
public ConfigLoader()
{
cwd = get_cwd();
file_separator = get_file_separator();
try
{
Properties c = new Properties();
InputStream in = this.getClass().getClassLoader().getResourceAsStream(cwd +
file_separator + "data" + file_separator + "configuration.properties");
c.load(in);
}
except (Exception e) { e.printStackTrace(); }
}
public String get_file_separator()
{
File f = new File("");
return f.separator;
}
public String get_cwd()
{
File cwd = new File("");
return cwd.getAbsolutePath();
}
For some reason, though, c.load(in);
causes a NullPointerException
. The exception comes from in == NULL
being true. I can't figure out why because
System.out.println(cwd + file_separator + "data" + file_separator +
"configuration.properties");
prints
/users/labnet/st10/jjb127/workspace/Brewer-Client/data/configuration.properties
which is the location of the file I'm wanting to use.
Thoughts?
Upvotes: 4
Views: 8312
Reputation: 4057
getResourceAsStream
is meant to search for files on the classpath and not for accessing the local file system. You will have to use FileInputStream
for this case.
InputStream in = new FileInputStream(cwd +
file_separator + "data" + file_separator + "configuration.properties");
Upvotes: 6