解析JSON时获取NullPointer异常

时间:2013-04-10 17:18:33

标签: android json nullpointerexception

我试图从webservices解析json,但我得到nullpointer异常。但是如果我在浏览器中点击url然后我得到结果但是在代码中它返回null。

这是我的网址: - http://www.expensekeeper.in/webservice/FetchBillCall.php?userid=7

这是我的代码: -

@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();
   }

   }

这是我的jsonParser类: -

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;

}   

这是我的堆栈跟踪: -

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)   

我不明白我的代码出了什么问题。为什么我从服务器获取空值。
请给我任何参考 提前致谢

1 个答案:

答案 0 :(得分:2)

修改

getJSONFromUrl(String str)JSONObject返回的更改类型更改为JSONArray

然后像这样直接创建JSONArray

JSONArray array=new JSONArray(json); // json是你的json字符串

getJSONFromUrl(String str)方法

中返回数组

您的服务返回JSONArray

基于上面的响应,您的服务返回JSONArray而不是JSONObject,因此当您尝试将JSONArray转换为JSONObject时,它也会抛出JSONException。

  JSONObject jsonObject = jsonParser.getJSONFromUrl(url);

所以这将返回null,

因为你的jsonObject是null而你正试图在这里访问它

dueDate = jsonObject.getString("duedate");

您将获得NullPointerException