1

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)
1

4 Answers 4

3

You're getting a NullPointerException because config is still null when you call the load method. You need to initialize your config object like this:

config = new Properties();

Probably best to put it right below your null check for it:

if( this.config == null ) 
{
    config = new Properties();
    ....
Sign up to request clarification or add additional context in comments.

Comments

1

You have just created a reference in this line Properties config; but have not created the object

Create an object like Properties config=new Properties()

Comments

0

Try with remove if condition ( if( this.config == null )). Final code is:

final class Main
{
    protected Properties config; 

    protected Properties getConfig()
    {

        this.config=new new Properties();
            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;
     }
}

Comments

0

The problem is that your config variable has not been initialized and hence is still null. So in your try/catch block you should add config = new Properties();

On a side note, there is an easier way to get the InputStream. We use the following technique (Java 6):

    InputStream inputStream = null;
    Properties config = null;
    try
    {
        config = new Properties();
        inputStream = this.getClass().getClassLoader().getResourceAsStream("path + resource name");
        if (inputStream != null)
        {
            config.load(inputStream);
        }
        else
        {
            throw new FileNotFoundException("property file '" + "path + resource name" + "' not found in the classpath");
        }
    }
    catch (IOException e)
    {
        throw new RuntimeException(e);
    }
    finally
    {
        if (inputStream != null)
        {
            try
            {
                inputStream.close();
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
        }
    }

Obviously with java 7 you could you the try with resources.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.