9

I have this function with some dead code, marked by Eclipse.

I have two lines that check a & b. Lines that check b are marked as null.

    public int[] runThis(List<Integer> buildIds, List<Integer> scenarios, boolean oflag) {

    int rating[] = new int[scenarios.size()];

    if(buildIds == null) {
        System.out.println("ERROR - Building ID list is null!");
        return null;
    }

    if(scenarios == null) {
        System.out.println("ERROR - Scenario list is null!"); //dead
        return null; //dead
    }

    return rating;      

}

Why does Ellipse make the two lines as dead? Any help? Thanks very much for your time.

jn1kk
  • 5,012
  • 2
  • 45
  • 72
  • 8
    Is there any other code in that method? Maybe you've been dereferencing `b` before, which tells Eclipse that `b` can't be `null` at this point. In that case you should also get a warning like "unnecessary null check" at the `if (b == null)` line. – Joachim Sauer Nov 07 '11 at 16:56
  • I don't know about *Eclipse* much but IntelliJ IDEA can analyze your code in real-time and warn you that some code is impossible to reach. In your case, if IntelliJ were to detect that *b* was never *null*, then it would warn you that these two lines can never be reached because the condition *b == null** is always false. – TacticalCoder Nov 07 '11 at 16:58
  • @Joachim: that particular warning is configureable in Eclipse's settings. I believe it's by default turned off. – BalusC Nov 07 '11 at 16:59
  • can you paste the whole method? – soulcheck Nov 07 '11 at 17:00
  • @BalusC: Oh, I didn't remember that. I probably turned it on as soon as I heard about it ;-) pretty useful these ones. – Joachim Sauer Nov 07 '11 at 17:17

1 Answers1

18

Because you've already called scenarios.size() in your array constructor. This guarantees scenarios isn't null or it will have thrown an exception by that point.

BenCole
  • 2,092
  • 3
  • 17
  • 26
  • Thank you so much for that explanation. I hadn't realized that by evaluating something earlier it's guaranteed NOT to get there because of prior NPE, so hence: "Dead code" warning. – atom88 Apr 03 '20 at 20:17