문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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.

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...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top