Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
            }
        }
    }
}
like image 493
User3 Avatar asked Aug 11 '15 10:08

User3


1 Answers

A variable declaration such as JSONObject jObj = ... must be inside a block.

Therefore either :

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

or

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

are valid.

like image 110
Eran Avatar answered Oct 13 '22 01:10

Eran