2

I'm getting dead code warning in eclipse from the if(line == null) statement to the r.close() statement.

BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));

int counter = 0;

while(true)
{
    String line = r.readLine();
    counter++;

    if(line.contains(" = "))
    {
        String[] keyVal = new String[2];

        keyVal[0] = line.substring(0, line.indexOf(" = ") - 1);
        keyVal[1] = line.substring(line.indexOf(" = ") + 3);

        buffer.add(keyVal);
    }
    else
    {
        new Exception("Invalid expression at line " + counter + " in file " + file.getPath()).printStackTrace();
    }

    if(line == null) // dead code here
    {
        break;
    }
}

System.out.println("here is a dead code");

r.close(); // another dead code
WW.
  • 23,793
  • 13
  • 94
  • 121
Gergő Gutyina
  • 129
  • 1
  • 12
  • 11
    Because if `line` were null, you'd have an exception at `line.contains(" = ")`. – shmosel Aug 20 '18 at 23:33
  • Well, if `line` is `null`, then `if(line.contains(" = "))` will cause a `NullPointerException` - to start with. Try and avoid `while(true)` and `break`, it's just messy, have an actual exit condition from your loop you can easily reason about. Also, you might like to have a look at [The try-with-resources Statement](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) as a better means for managing your resources – MadProgrammer Aug 20 '18 at 23:33
  • 1
    Also, if `line.contains(" = ")` is false, you throw an exception, so a whole bunch of code never runs – MadProgrammer Aug 20 '18 at 23:35
  • 4
    Not the answer, but the `new Exception...` line should start with `throw`. At the moment, the code creates an exception and does nothing with it. – Alex Taylor Aug 20 '18 at 23:36
  • 2
    @AlexTaylor, actually, there's a `printStackTrace()` at the end of that line. However, this is kind of an abomination. It misses the point of throwing and catching exceptions. If OP wants to print an error message, they should just print an error message. – Dawood ibn Kareem Aug 20 '18 at 23:41
  • The code outside the while is also dead, because the break condition is dead and the while is an infinite loop, so the code will never be reached. – Ayrton Aug 20 '18 at 23:42
  • @DawoodibnKareem Yes, I missed that. Which means I change it to a recommendation: @Gergő Gutyina change it to a `throw`, and remove the `printStackTrace()`. – Alex Taylor Aug 20 '18 at 23:46

2 Answers2

5

You are operating on line with if(line.contains(" = ")). If line was null, a NullPointerException would be thrown, which you are not catching. Therefore, the break; statement can't be reached. If the if(line == null) statement is reached, it's not null. Logically, break; will never be executed, so it's unreachable. It appears that Eclipse is detecting this. Note that this will still compile with javac just fine; this is just Eclipse's warning.

Furthermore, if the break; is unreachable, then there is no way to end while(true), so anything beyond the while block is unreachable. So Eclipse warns on this too.

The usual way of reading a file line-by-line, checking the line for null to indicate end of the stream, is

String line;
while ( (line = r.readLine() ) != null)
{
    // rest of processing here
}

It's not the cleanest code, with an assignment in the same expression as an equality check, but it's the usual way of doing this kind of read-and-check operation.

This way the while statement can end normally, and then there is no need to check for null later. That also makes any code following the while block reachable.

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

Put

if(line == null) 
{
    break;
}

before the

if(line.contains(" = "))

reason it is a dead code is because both if else statements can result in an exception , making any code after that a dead code.

Madhu V Rao
  • 917
  • 1
  • 12
  • 24
  • 2
    Even better: he could put that check on the while itself, like `while((line = r.readLine()) != null)` – Ayrton Aug 20 '18 at 23:45