Sandip Armal Patil
Sandip Armal Patil

Reputation: 5895

Getting NullPointer Exception while parsing JSON

I am trying to parse json from webservices but i get nullpointer exception. But if i hit url in browser then i get results but in code it's returning null.

Here is my url:- http://www.expensekeeper.in/webservice/FetchBillCall.php?userid=7

Here is my code:-

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    new LongOperation().execute("");

}

private class LongOperation extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        String url = "http://www.expensekeeper.in/webservice/FetchBillCall.php?userid=7";
         JSONParser jsonParser = new JSONParser();
         JSONObject jsonObject = jsonParser.getJSONFromUrl(url);
         try {
             //JSONArray jsonArray = jsonObject.getJSONArray("");
            dueDate = jsonObject.getString("duedate");
            description = jsonObject.getString("description");
            status = jsonObject.getString("status");
            remarks = jsonObject.getString("remarks");
        } catch (JSONException e) {
        e.printStackTrace();
        }
          return "Executed";
    }  
    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(MainActivity.this, dueDate+description+status+remarks, Toast.LENGTH_LONG).show();
   }

   }

Here is my jsonParser class:-

public JSONObject getJSONFromUrl(String str) {

    HttpURLConnection conn = null;
    StringBuilder jsonResults = new StringBuilder();
    try {
        URL url = new URL(str);
        conn = (HttpURLConnection) url.openConnection();
        InputStreamReader in = new InputStreamReader(conn.getInputStream());

        // Load the results into a StringBuilder
        int read;
        char[] buff = new char[1024];
        while ((read = in.read(buff)) != -1) {
            jsonResults.append(buff, 0, read);
        }
        json = jsonResults.toString();
    } catch (MalformedURLException e) {
        Log.e(LOG_TAG, "Error processing Places API URL", e);
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error connecting to Places API", e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
     try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
     return jObj;

}   

And here is my stacktrace:-

04-10 17:15:16.425: E/AndroidRuntime(1388): Caused by: java.lang.NullPointerException
04-10 17:15:16.425: E/AndroidRuntime(1388):     at com.example.jsonsample.MainActivity$LongOperation.doInBackground(MainActivity.java:40)
04-10 17:15:16.425: E/AndroidRuntime(1388):     at com.example.jsonsample.MainActivity$LongOperation.doInBackground(MainActivity.java:1)   

I don't understand what going wrong in my code. Why i am getting null value from server.
Please give me any reference.
Thanks in advance

Upvotes: 1

Views: 2964

Answers (1)

Pragnani
Pragnani

Reputation: 20155

Edit:

Change type returned by getJSONFromUrl(String str) from JSONObject to JSONArray.

And then create JSONArray directly like this

JSONArray array=new JSONArray(json); // json is your json string

return array in the getJSONFromUrl(String str) method


Your Service returning JSONArray

Based on the above response, your service returning JSONArray , not JSONObject, so it will also throw JSONException, when you try to convert JSONArray to JSONObject.

  JSONObject jsonObject = jsonParser.getJSONFromUrl(url);

So this will return you null,

as your jsonObject is null and you are trying to access it here

dueDate = jsonObject.getString("duedate");

You will get NullPointerException

Upvotes: 2

Related Questions