0
private static Properties getProperties(File file)
    {
        InputStream in = null;
        try
        {
            in = new FileInputStream(file);
            return loadProperties(in);
        }
        catch (IOException ex)
        {
            return null;
        }
        finally
        {
            if (in != null)
            {
                try
                {
                    in.close();
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                    // ignore
                }
            }
        }
    }

When I click on the save button to save my configuration then I get null pointer exception. I tried to debug the code, In that time I have found the value of object in is null. I do not understand why?

I have also checked that the path which I have pass as argument file is correct

5
  • 4
    What line does in = null? Or is this function just returning null because of an exception that you are ignoring? Commented Jul 9, 2014 at 15:47
  • I have initialized reference variable to the null after starting the debugger then it first execute the try block first statement only in = new FileInputStream(file) after that value of in found null it go to the catch block.... I am new in developemnt so could please help me to remove this error Commented Jul 9, 2014 at 15:56
  • Never silently swallow exceptions. Commented Jul 9, 2014 at 16:00
  • Please add the null stack trace and indicate which line in your code corresponds to the error line in the stack trace. Commented Jul 9, 2014 at 16:08
  • 2
    This looks like a textbook example of when to use try-with-resources btw Commented Jul 9, 2014 at 16:25

2 Answers 2

1

I beileve there is only one reason the NPE happens -- it didn't find the file with the path you give.

I have also checked that the path which I have pass as argument file is correct

Sometimes even you're thinking you provided a correct file path, depends on the directory hierarchy, program may refer tt relative path which may be different from the path you give. Then you got in = null.

try ClassLoader.getSystemResourceAsStream() instead of FileInputStream(), you can put your file under class path.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to make sure that the file object is exists using file.exists(). If the file exist it will return true. If the file object is null it will also raise NullPointerException.

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.