0

I got Null pointer exception when reading data from file.if its returning a junk value how to handle that. if i didn't give trim giving some junk value. My code is:

BufferedReader br = null;
try {           
    String sCurrentval = "";
    br = new BufferedReader(new FileReader("filepath"));
    while ((sCurrentval = br.readLine()) != null) {
        System.out.println("Reading from File "+sCurrentval);
    }
    if(sCurrentval != null){
        sCurrentval = sCurrentval.trim();
    }
    System.out.println("outside :  Reading from File "+sCurrentval);
    if(sCurrentval != null && !sCurrentval.equalsIgnorecase("")){
        try{
            val = Integer.parseInt(sCurrentval.trim());
        }catch(Exception e){
            e.printStackTrace();
        }
    }else{
        System.out.println("Reading Value  null ");
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (br != null)br.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
5
  • 2
    Which line is giving the NullPointerException? You should be able to see that in the stacktrace. Commented Aug 27, 2013 at 9:47
  • 1
    The while will skip over the whole file. sCurrentval can only be null afterwards Commented Aug 27, 2013 at 10:01
  • @ Daniel Lerps - its absolute path of file Commented Aug 27, 2013 at 10:04
  • Write the stack trace Commented Aug 27, 2013 at 10:06
  • See the updated answer it may help you Commented Aug 27, 2013 at 10:35

1 Answer 1

1

Your BufferedReader br = null; with in the try. But your finally also using the same variable br.

 try
    {
    //
    BufferedReader br = null; // declared with in try
    //
    }
    finally {
    try {
    if (br != null) // In this line the br is not identified 
     br.close();
    } catch (IOException ex) 
    {
    ex.printStackTrace();
    }

Try to declare the BufferReader outside the try

BufferedReader br = null;

And then your while loop is only for the printing the value of the variable. Include the below if else condition within the while and then try the below code.

while ((sCurrentval = br.readLine()) != null)
            {
                System.out.println("Reading from File " + sCurrentval);
                if (sCurrentval != null && !sCurrentval.trim().isEmpty())
                {
                    try
                    {
                        val = Integer.parseInt(sCurrentval.trim());
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                else
                {
                    System.out.println("Reading Value  null ");
                }
            }
Sign up to request clarification or add additional context in comments.

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.