Note: Please forgive me, if I have wrong in english.
I'm learning java! and now I try to load properties from file to java.util.Properties object...
but I got an exception. I get filename by getClass().getResource("path/to/resource").toFile() and make a File object from it; then read content. but when I send the InputStream of file to 'load' method, get a NullPointerException.
This is my code:
final class Main
{
protected Properties config;
protected Properties getConfig()
{
if( this.config == null )
{
String filename = this.getClass().getResource("/tiny.properties").getFile();
System.out.print( filename+ "\n" );
File f = new File( filename );
try(InputStream propsFile = new FileInputStream( f ) )
{
int ch = 0;
while( (ch = propsFile.read() ) != -1 )
{
System.out.print((char) ch); // I can get ALL content of File Here
}
this.config.load( propsFile ); // But here I got NullPointerException!
}
catch(IOException exc)
{
assert true : "Can't read properties file!";
}
}
return this.config;
}
}
and My exception:
Exception in thread "main" java.lang.NullPointerException
at ir.teanlab.japp1.mavenproject1.Main.getConfig(Main.java:43) // this.config.load( ... )
at ir.teanlab.japp1.mavenproject1.Main.getSqlConnection(Main.java:57)
at ir.teanlab.japp1.mavenproject1.Main.<init>(Main.java:67)
at ir.teanlab.japp1.mavenproject1.App.main(App.java:15)