1

I have the following code:

try {
    if ((jsonData = new JSONObject(getJSONString(request))) != null) {
        if (jsonData.has("country_code")) {
            user_id = Integer.valueOf(request.getParameter("user_id"));
            jobjcountries = new OpenNCPApi().getCountryListAttritube(jsonData.getString("country_code"), "en");
            jObj.put("status", 1);
            jObj.put("attributes", jobjcountries);
            return jObj.toString();
        } else {
            jObj.put("status", 0);
            jObj.put("error", "Unfilled inputs");
            return jObj.toString();
        }
    }
    //request is null
    else {
        jObj.put("status", 0);
        jObj.put("error", "Unfilled inputs");
        return jObj.toString();
    }
} catch (Exception e) {
    e.printStackTrace();
    jObj.put("status", 0);
    jObj.put("error", "Problem while calling ncp service");
    return jObj.toString();
} finally {...}

For the following code I get a warning of dead code.

   else {
            jObj.put("status", 0);
            jObj.put("error", "Unfilled inputs");
            return jObj.toString();
        }

However, return jObj.toString(); is needed to be there and I need to have status and error values. I've added this else statement to satisfies the case when (jsonData = new JSONObject(getJSONString(request))) is null.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
zinon
  • 4,427
  • 14
  • 70
  • 112

2 Answers2

1

I've added this else statement to satisfies the case when (jsonData = new JSONObject(getJSONString(request))) is null.

The new JSONObject(getJSONString(request))) will never be null. As one can conclude by reading JLS section 15.9.4:

The value of a class instance creation expression is a reference to the newly created object of the specified class. Every time the expression is evaluated, a fresh object is created.

This statement

(jsonData = new JSONObject(getJSONString(request))) != null)

will either evaluate to true or throw an Exception.

If it evaluate to true then one of the following branches will be executed:

if (jsonData.has("country_code")) {
    user_id = Integer.valueOf(request.getParameter("user_id"));
    jobjcountries = new OpenNCPApi().getCountryListAttritube(jsonData.getString("country_code"), "en");
    jObj.put("status", 1);
    jObj.put("attributes", jobjcountries);
    return jObj.toString();
    
} else {
    jObj.put("status", 0);
    jObj.put("error", "Unfilled inputs");
    return jObj.toString();
}

If it throws an exception then:

} catch (Exception e) {
    e.printStackTrace();
    jObj.put("status", 0);
    jObj.put("error", "Problem while calling ncp service");
    return jObj.toString();

will be executed.

In either the aforementioned cases the block:

else {
    jObj.put("status", 0);
    jObj.put("error", "Unfilled inputs");
    return jObj.toString();
}

will never be reach, hence the warning about dead code.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
1

You are missing that ((jsonData = new JSONObject(getJSONString(request))) != null) never will be null as you create a new object reference. The value of the object could be null. But in that case, the else{} statement will not be reached and can safely be deleted.

FunnyO
  • 383
  • 2
  • 20
  • When creating a new Object, you normally have an Object-Constructor with Parameters. Those could be null. – FunnyO Jan 30 '21 at 13:05