为什么我在这个Volley onResponse中收到NullPointerException错误?

时间:2017-05-30 10:11:16

标签: android json android-volley

在我的应用中,我发布了用户的电话号码并获取了一个JSON数组作为响应,格式为[{"category" : "category", "name" : "name", "phone" : "phone", "comment" : "comment"}, etc etc ..... ]

我正在尝试在ListView中格式化此JSON数组响应但我收到此错误并且我的应用程序关闭。帮助将不胜感激。

    java.lang.NullPointerException
at appname.onResponse(ListView.java:130)
at appname.onResponse(ListView.java:83)

以下是我的代码:

  StringRequest stringRequest = new StringRequest(Request.Method.POST, SelectUserReviews_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(ListView.this, response, Toast.LENGTH_LONG).show();

                        JsonArrayRequest movieReq = new JsonArrayRequest(SelectUserReviews_URL,

                                new Response.Listener<JSONArray>() {
                                    @Override
                                    public void onResponse(JSONArray response) {
                                        Log.d(TAG, response.toString());
                                        hidePDialog();

                                        // Parsing json
                                        for (int i = 0; i < response.length(); i++) {
                                            try {

                                                JSONObject obj = response.getJSONObject(i);
                                                Review review = new Review();
                                                review.setCategory(obj.getString("category"));

                                                review.setName(obj.getString("name"));
                                                review.setPhone(obj.getString("phone"));
                                                review.setComment(obj.getString("comment"));

                                                reviewList.add(review);

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

                                        }

                                        // notifying list adapter about data changes
                                        // so that it renders the list view with updated data
                                        adapter.notifyDataSetChanged();
                                    }
                                }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                VolleyLog.d(TAG, "Error geting user info: " + error.getMessage());
                                hidePDialog();

                            }
                        });

                        // Adding request for getting user info to request queue
                        AppController.getInstance().addToRequestQueue(movieReq);
                    }


                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error getting user: " + error.getMessage());
                        hidePDialog();

                    }

                }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(KEY_PHONENUMBER_USER, phoneNoofUser);
                return params;

            }

        };
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

    }

第130行是:

AppController.getInstance().addToRequestQueue(movieReq);

第83行是:

   new Response.Listener<String>() {

0 个答案:

没有答案