ParseJSON

时间:2016-04-24 21:54:13

标签: java android

当我试图解析json时,它抛出一个空指针异常。当我试图显示值时,也没有显示任何内容。

代码:

public class ParseJSON {
    //public static String[] ids;
    public static String[] names;
    public static String[] message;

    public static final String JSON_ARRAY = "users";
   // public static final String KEY_ID = "id";
    public static final String KEY_NAME = "username";
    public static final String KEY_EMAIL = "message";

    private JSONArray users = null;

    private String json;

    public ParseJSON(String json){
        this.json = json;
    }

    protected void parseJSON(){
        JSONObject jsonObject=null;
        try {
            jsonObject = new JSONObject(json);
            users = jsonObject.getJSONArray(JSON_ARRAY);

            //final String TAG = events.class.getSimpleName();
            //Log.d(TAG, "username value: \n" + KEY_NAME);


            for(int i=0;i<users.length();i++){
                JSONObject jo = users.getJSONObject(i);
               // ids[i] = jo.getString(KEY_ID);
                names[i] = jo.getString(KEY_NAME);
                message[i] = jo.getString(KEY_EMAIL);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
} 

但是当我试图显示解析值时,我在一个单独的页面中显示它,如果我可以尝试使用LOGD显示值,它会显示值。

list_view.java

   private void sendRequest(){


        StringRequest stringRequest = new StringRequest(JSON_URL,
                new Response.Listener<String>() {



                    @Override
                    public void onResponse(String response) {
                        showJSON(response);
                    }
                },


                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(list_view.this,error.getMessage(),Toast.LENGTH_LONG).show();
                    }
                });

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




    private void showJSON(String json){

       final String TAG = events.class.getSimpleName();
       Log.d(TAG, "showJSON: \n" + json);

        ParseJSON pj = new ParseJSON(json);
        pj.parseJSON();
        CustomList cl = new CustomList(this,ParseJSON.names,ParseJSON.message);
        listView.setAdapter(cl);
    }

    @Override
    public void onClick(View v) {
        sendRequest();
    }
}

这是错误;

04-24 21:49:39.449 15992-15992/? D/events: showJSON: 
                                           {"users":[{"username":"varun","message":"hello word, my first message "},{"username":"varun","message":"hello word, my first message "},{"username":"rahul","message":"hello world"},{"username":"rahul","message":"world: its cool"},{"username":"rahul","message":"awesom"},{"username":"rahul","message":"ranom"},{"username":"rahul","message":"randimagain"},{"username":"rahi","message":"is it me again"},{"username":"rahull","message":"hi rahul"},{"username":"rahul","message":"world, am bacj"},{"username":"rahul","message":"hello parents"},{"username":"","message":""},{"username":"","message":""},{"username":"","message":""},{"username":"","message":""},{"username":"","message":""},{"username":"varun","message":"google world"},{"username":"rahul","message":"ollaaa"},{"username":"rahul","message":"ollaaa"},{"username":"rahul","message":"where are others"},{"username":"rahul","message":"where are others"},{"username":"varun","message":"oil spil"},{"username":"varun","message":"phone"},{"username":"vatun","message":"tttt"},{"username":"varun","message":"poiuytrew"},{"username":"rahui","message":"yregh"},{"username":"varun","message":"huiokgddfv"},{"username":"rahi","message":"poiutghklnbfd"},{"username":"rahui","message":"qwerty"},{"username":"varub","message":"hi eat pineapple"},{"username":"varun","message":"hi prabh"},{"username":"rahul","message":"hi world"},{"username":"ggjdjjf","message":"yhvdhhj"},{"username":"tgggg","message":"hhhh"},{"username":"rahul","message":"lqvrl"},{"username":"hhhh","message":"ghh"},{"username":"tff","message":"gg"},{"username":"hfdhn","message":"hnnnme"},{"username":"popo","message":"plpl"},{"username":"rahulpop","message":"op music"},{"username":"rtrt","message":"tyty"},{"username":"hlpo","message":"opop"},{"username":"hlpoghhj","message":"opop"},{"username":"tt","message":"hhh"},{"username":"ttere","message":"hhh"},{"username":"gytgjyg","message":"lajdhd"},{"username":"topo","message":"ptpt"},{"username":"jgj","message":"rtr"},{"username":"ry","message":"yr"},{"username":"ry","message":"yr"},{"username":"y","message":"q"},{"username":"u","message":"t"},{"username":"tt","message":"pp"},{"username":"q","message":"z"},{"username":"w","message":"v"},{"username":"e","message":"e."},{"username":"ui","message":"ui"},{"username":"yi","message":"yi"},{"username":"gg","message":"g"},{"username":"e","message":"e"},{"username":"h","message":"hh"},{"username":"j","message":"j"},{"username":"j","message":"k"},{"username":"v","message":"v"}]}
04-24 21:49:39.459 15992-15992/? D/AndroidRuntime: Shutting down VM
04-24 21:49:39.459 15992-15992/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40c1ea68)
04-24 21:49:39.529 15992-15992/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   java.lang.NullPointerException
                                                       at com.weavearound.schools.weavearound.ParseJSON.parseJSON(ParseJSON.java:43)
                                                       at com.weavearound.schools.weavearound.list_view.showJSON(list_view.java:76)
                                                       at com.weavearound.schools.weavearound.list_view.access$000(list_view.java:22)
                                                       at com.weavearound.schools.weavearound.list_view$1.onResponse(list_view.java:51)
                                                       at com.weavearound.schools.weavearound.list_view$1.onResponse(list_view.java:45)
                                                       at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
                                                       at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
                                                       at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                       at android.os.Handler.handleCallback(Handler.java:605)
                                                       at android.os.Handler.dispatchMessage(Handler.java:92)
                                                       at android.os.Looper.loop(Looper.java:137)
                                                       at android.app.ActivityThread.main(ActivityThread.java:4517)
                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:511)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
                                                       at dalvik.system.NativeStart.main(Native Method)
04-24 21:49:39.559 403-420/? E/android.os.Debug: !@Dumpstate > dumpstate -k -t -n -z -d -o /data/log/dumpstate_app_error

1 个答案:

答案 0 :(得分:0)

你的问题是字符串数组没有初始化。

{{1}}