0

I'm working on a Java project to Add each Integer to the one in the next line until there's no lines to read in the file. So to be able to add it I have to use Integer.parseInt(...) to the line then add it. P.S : the for loop will just skip two lines which contain the header of the file. And all the string are refering to numbers which Integer.parseInt() accepts.

Here's the full exception error :

Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at prog.Result(prog.java:93)
at prog.main(prog.java:56)

The code resulting in the exception is :

public static void Result() throws IOException
    {
        FileReader fileReader = new FileReader(dir+"/"+log_file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        int i;
        for (i=0;i<=2;++i)
        {
            bufferedReader.readLine();
        }
        int result =0;
        while (bufferedReader.readLine() != null)
        {
            result += Integer.parseInt(bufferedReader.readLine());

        }
        System.out.println("The Result Is : " + result);

    }
3
  • Whatever it's reading isn't a number. Check your data source. Commented Sep 13, 2013 at 2:53
  • It's actually a number , here's the actual log.txt file source used : 15 13 20 20 (each number is in a line) Commented Sep 13, 2013 at 2:54
  • Try to debug and you will see the error! Commented Sep 13, 2013 at 2:55

3 Answers 3

5

This block is actually reading two lines, not one.

while (bufferedReader.readLine() != null)
{
    result += Integer.parseInt(bufferedReader.readLine());
}

The error occurs when the last line is read in the while condition check, and the inner part of the block will then read null, since there are no more lines to be read.

It's idiomatic to write loops like this as:

String line;
while ((line = bufferedReader.readLine()) != null)
{
    result += Integer.parseInt(line);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Using this I only get Result = 20 ; as it should equal 40 because the two lines that should be added contain : 20 20
Ok I got it , my mistake this : for (i=0;i<=2;++i) should be : for (i=0;i<2;++i) , so the for loop is only executed 2 times not 3.
5

I think this is your problem:

    while (bufferedReader.readLine() != null)
    {
        result += Integer.parseInt(bufferedReader.readLine());

    }

You're calling readLine() twice there. You should store the initial result in a variable, and the reuse that result in the parseInt() call.

2 Comments

Can you please clearify how should I store the initial result in a variable and do the addition as wanted.
@user2774643 - Do it the way Mark Elliot suggests in his answer.
0

First: you should check data in your file to ensure that all lines are number.

Second: you should try catch at the line as below

try {
    result += Integer.parseInt(bufferedReader.readLine());
} catch(Exception ex)

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.