4

I can't see why Eclipse gives me a dead code warning for the code in the second if condition:

 boolean frameErreicht = false;
  while (!frameErreicht) {
        String line = reader.readLine();
        if (line.matches("@\\d*")) {
            reader.mark(reader.getLineNumber() - 1);
            reader.setLineNumber(reader.getLineNumber() - 1);
            frameErreicht = true;
        }
        if (line == null)
            throw new IOException("Keine Angaben zu Frames im Eingabestrom");
    }

The jdoc of the readLine() method of LinenumberReader says that it will return null if the end of the stream is reached, so if the match is not found throughout the whole text (line == null) it should throw an exception.
But what's wrong?

2
  • Is it because you're checking line.matches? It can't be null when it hits the lower line as that access will cause a throw? Move the null check further up before the call to matches.
    – SpaceBison
    Mar 5, 2013 at 18:53
  • 2
    In other words, you haven't ever run it? Why asking before running?
    – BalusC
    Mar 5, 2013 at 18:54

3 Answers 3

14

If line had been null, line.matches("@\\d*") would have thrown a NullPointerException

1
  • @mellamokbtheWise same thing :)
    – bsiamionau
    Mar 5, 2013 at 18:53
2

Its dead since you previously dereferenced the pointer 'line' in line #4 which guarantees that it is NOT NULL or else you would get NullPointerException.

1
  • 1
    Please just vote up the existing answer if you think it's a good answer rather than posting a duplicate.
    – mellamokb
    Mar 5, 2013 at 18:57
1

First:

boolean found = false;
String line = null;
while ((line=reader.readLine())!=null) {
    if (line.matches("@\\d*")) {
        reader.mark(reader.getLineNumber() - 1);
        reader.setLineNumber(reader.getLineNumber() - 1);
        found=true;
        break;
    }
}
if(!found) throw new IOException("Keine Angaben zu Frames im Eingabestrom");

And you are not doing this check at each line...

This code shouldn't return any dead code...

1
  • 3
    Why are you removing OP's intent to throw an IOException? The solution is rather to just swap the if's.
    – BalusC
    Mar 5, 2013 at 18:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.