Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException while returning json object
    primarykey
    data
    text
    <p>I'm trying to post json object, then try to receive json object. Through sniffer it show that post and get successful works. But receiving object is always null. I think it's because of my carelessness, so I hope you help me with it.</p> <pre><code> try { DefaultHttpClient httpclient = new DefaultHttpClient(ccm, params); HttpPost httppostreq = new HttpPost(URL); StringEntity se; se = new StringEntity(jsonObjSend.toString()); //se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); se.setContentType("application/json;charset=UTF-8"); //se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8")); httppostreq.setEntity(se); //Setting headers HttpResponse response = httpclient.execute(httppostreq); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); Header contentEncoding = response.getFirstHeader("Content-Encoding"); if (contentEncoding != null &amp;&amp; contentEncoding.getValue().equalsIgnoreCase("gzip")) { instream = new GZIPInputStream(instream); } // convert content stream to a String String resultString= convertStreamToString(instream); instream.close(); resultString = resultString.substring(1,resultString.length()-1); // Transform the String into a JSONObject JSONObject jsonObjRecv = new JSONObject(resultString); return jsonObjRecv; } } catch (Exception e) { e.printStackTrace(); } return null; private static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } </code></pre> <p>and here is a part of Activity where I keep getting NullPointerException error at line <code>JSONObject jsonObjRecv = JSONPost.SendHttpPost(URL, jsonObjSend);</code> :</p> <pre><code>// Send the HttpPostRequest and receive a JSONObject in return JSONObject jsonObjRecv = JSONPost.SendHttpPost(URL, jsonObjSend); try { jsonObjRecv.getJSONObject("id"); //Here I got the error } catch (JSONException e) { e.printStackTrace(); } </code></pre> <p>When I changed type jsonObjRecv to String it started to work, but I need JSON type for it.</p> <p>Error:</p> <pre><code>02-01 21:39:01.673: ERROR/AndroidRuntime(20855): FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Phone/com.Phone.MainActivity.OtherActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797) at android.app.ActivityThread.access$2300(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:143) at android.app.ActivityThread.main(ActivityThread.java:4914) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.Phone.MainActivity.OtherActivity.onCreate(OtherActivity.java:47) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745) ... 11 more </code></pre> <p>Thank you for your response.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload