Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dead Code Warning in IF Statement

Tags:

java

warnings

I don't know why eclipse warn me "Dead Code" in this code of mine:

    PreparedStatement p = conn.prepareStatement("SELECT name FROM user WHERE age = ? , city = ?");
    p.setInt(1, (String) parameters.get("age"));
    p.setString(2, (String) parameters.get("city"));

    if (p == null) {
        log.warn("User is not available");
        return results;
    }

the warning was in the log, can anybody tell me the reason of this? thank you.

like image 272
Muhammad Kholid B Avatar asked Feb 19 '23 23:02

Muhammad Kholid B


1 Answers

p can not be null at that point, simply because you already called setInt and setString on p and if it were null there, then it would have thrown a NullPointerException, thus never reaching your if.

Also note that according to its documentation, preparedStatement can't ever return null. It can only return a valid statement or throw an exception. In the latter case your if will also not be reached. That fact, however, is not checked by the compiler (because a broken implementation of prepareStatement could theoretically return null).

like image 147
Joachim Sauer Avatar answered Feb 26 '23 11:02

Joachim Sauer