Why is this not a statement

I am posting some data to a webservice here is the snippet:

try {
    response = UniversalHttpUrlConnection.postJSONObject(ctx.getResources().getString(R.string.delete_one), UniversalHashMapToJSONObjectParams.createObjectForParams(deleteMap));
    System.out.println("Response from del task" + response);

    if(response != null) 
        JSONObject jObj = new JSONObject(response);

    System.out.println("Deletion" + DatabaseHandler.getInstance(ctx).removeReceipt(receipt));

} catch (JSONException report) {
    report.printStackTrace();
}

My IDE is complaining about:

if(response != null) 
    JSONObject jObj = new JSONObject(response);

The exact statement being: Not a statement - Why is this happening, am I missing something here?

if I use:

if (response != null) {
    JSONObject jObj = new JSONObject(response);
}

The IDE does not complain.


For details here is my complete method:

public static void deleteReceipt(Receipt receipt, Context ctx) {

    SessionManager sessionManager;
    sessionManager = new SessionManager(ctx);
    HashMap<String, String> map = sessionManager.getUserDetails();
    String email = map.get("email");

    if (CheckConnection.isNetworkConnected(ctx)) {

        System.out.println("local id " + receipt._id);
        Receipt rec = DatabaseHandler.getInstance(ctx).getReceipt(receipt._id);

        String webReceiptId = rec.web_receipt_id;
        System.out.println("WebReceipt ID = " + webReceiptId);
        if (webReceiptId.equalsIgnoreCase("dummy")) {
            // Delete from local
            System.out.println("Deletion" + DatabaseHandler.getInstance(ctx).removeReceipt(receipt));
        } else {
            // delete from server
            // delete from local
            HashMap<String, String> deleteMap = new HashMap<>();
            deleteMap.put("email", email);
            deleteMap.put("type", "Android");
            deleteMap.put("key", Utility.getencryptkey());
            deleteMap.put("task_id", webReceiptId);
            deleteMap.put("receipt_id", String.valueOf(receipt._id));

            String response = null;
            try {
                response = UniversalHttpUrlConnection.postJSONObject(ctx.getResources().getString(R.string.delete_one),
                        UniversalHashMapToJSONObjectParams.createObjectForParams(deleteMap));
                System.out.println("Response from del task" + response);

                if (response != null) {
                    JSONObject jObj = new JSONObject(response);
                }

                System.out.println("Deletion" + DatabaseHandler.getInstance(ctx).removeReceipt(receipt));

            } catch (JSONException report) {
                report.printStackTrace();
            }
        }
    }
}
Jon Skeet
people
quotationmark

What you've got is a local variable declaration - and that isn't allowed on its own as a statement... because it's pointless. Why declare a variable that you're not going to be able to use, because it's immediately out of scope? The compiler is trying to stop you from making a mistake, basically.

In JLS terms, there's a Statement and a Block - a Block contains BlockStatements, which are statements, class declarations or local variable declarations... and local variable declarations aren't statements.

people

See more on this question at Stackoverflow