If line had been null, line.matches("@\\d*")
would have thrown a NullPointerException
Dead code warning in while loop with 2 if's
Вопрос
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?
Решение
Другие советы
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...
Не связан с StackOverflow