Why is this not a statement

I put some data in the webservice, here is a 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 complains:

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

Exact statement: Not an expression . Why is this happening, am I missing something?

if i use:

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

IDE does not complain.


More 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(); } } } } 
+5
source share
3 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); 

.

+12
source

What you have is a declaration of a local variable - and this is not permitted as a statement ... because it is pointless. Why declare a variable that you cannot use because it immediately goes out of scope? The compiler is trying to stop you from making a mistake, basically.

In terms of JLS, there is a Statement and a Block - a block contains BlockStatements, which are statements, class declarations, or local variable declarations ... and local variable declarations are not instructions.

+12
source

In terms of JLS, there is an operator and a block - a block contains BlockStatements, which are statements, class declarations, or local variable declarations ... and local variable declarations are not instructions.

0
source

All Articles