'JSON object showing null pointer exception

I want to post data to the url and getting Null pointer exception My JSON URL contains

{
  "Details":
    [
      {
        "Status":"NO UPDATES"
      }
    ]
}

I'm getting the error the line:

String status = object.getString("Status").trim(); //error Line

Full code:

btnPost = (Button)findViewById(R.id.btnPost);

    btnPost.setOnClickListener(this);
    btnPost.setOnClickListener(new OnClickListener()
    {
        @SuppressWarnings("null")
        @Override
        public void onClick(View arg0)
        {
            try
            {
                String postReceiverUrl = "http://";
                Log.v(TAG, "postURL: " + postReceiverUrl);
                // HttpClient
                @SuppressWarnings("resource")
                HttpClient httpClient = new DefaultHttpClient();
                // post header
                HttpPost httpPost = new HttpPost(postReceiverUrl);

                jsonobject.put("IDNo", IDNo.getText().toString());
                jsonobject.put("Position", Position.getText().toString());
                jsonobject.put("Data", Data.getText().toString());

                HttpResponse response = httpClient.execute(httpPost);

                String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();

                Log.v("jsonResult",jsonResult);

                JSONObject object = new JSONObject(jsonResult);

                String status = object.getString("Status").trim();

                Toast.makeText(getBaseContext(), "Please wait...",100).show();

                if(status.toString().equals("SUCCESS"))
                {
                    Intent i = new Intent(LoginPage.this,MainActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
                if(status.toString().equals("FAILED"))
                {
                    Toast.makeText(getBaseContext(), "Wrong Credentials",100).show();
                }

                else
                {
                    Toast.makeText(getBaseContext(), "Details Inserted",100).show();
                }

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

            catch (JSONException e)
            {
                e.printStackTrace();
            } 
            catch (ClientProtocolException e)
            {
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }   


        }

    });


Solution 1:[1]

your code should be like this

 JSONObject object = new JSONObject(jsonResult);

            JSONOArray details = object.getJSONArray("Details");

           for (int i = 0; i < details.length(); i++) {
             JSONObject c = details.getJSONObject(i);

            String status = c.getString("Status");
            }
            Toast.makeText(getBaseContext(), "Please wait...",100).show();

Solution 2:[2]

Do proper parsing of the JSON as , you see yours response:-

 { "Details":[{"Status":"NO UPDATES"}]}

So Firstly try to make the object of the JSONObject than after the JSONArray , look at below example:-

JSONObject jsonObject = new JSONObject(jsonResult);
JSONArray detailsArray = jsonObject.getJSONArray("Details");

String status = dataArray.getJSONObject(0).getString("status");

 Toast.makeText(getBaseContext(), "Please wait...",100+status).show();

Solution 3:[3]

Modift String status = object.getString("Status").trim(); to

String status = object.get("Details").getAsJsonArray()[0].getString("Status").trim();

Solution 4:[4]

You are using getString("Status") which may return null if key is not available in JSON. I'd suggest you to use optString("Status") it'll return blank String if key is not available.

JSONObject jsonObject = new JSONObject(stringJSON);
JSONArray jsonArray = jsonObject.optJSONArray("Details");
if(jsonArray != null){
   for(int i = 0; i < jsonArray.length(); i++){
     JSONObject jObject = jsonArray.optJSONObject(i);
     if(jObject != null){
       String strStatus = jObject.optString("Status");
     }
   }
}

Solution 5:[5]

Try this code

 JSONObject object = new JSONObject(jsonResult);
 JSONArray array=object.getJsonarray("Details");
 for(int i=0;i<array.length();i++)
  {

   JSONObject innerObject=array.getJsonObject(i);
   String s= innerObject.getString("Status");

  }

Solution 6:[6]

If at any point in your code, org.json.JSONObject json_object becomes null and you wish to avoid NullPointerException (java.lang.NullPointerException), then simply check as following:

if(json_object == null) {
   System.out.println("json_object is found as null");
  }
  else {
       System.out.println("json_object is found as not null");
  }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 curiousMind
Solution 2 Ravindra Kushwaha
Solution 3 Bhargav
Solution 4 Vicky Thakor
Solution 5 Ravindra Kushwaha
Solution 6 UdayanBKamble