空指针异常新JSONobject(String)

时间:2015-04-15 21:22:15

标签: android json parse-platform nullpointerexception broadcastreceiver

我有一个广播接收器,它从onRecieve方法的intent中得到一个JSONObject。然后它从JSONObject获取一些数据。

try {
            Bundle bundle = intent.getExtras();
            jsonData = bundle.getString("com.parse.Data");

            object = new JSONObject(jsonData);
            action = object.getString("action");
            id = object.getString("id");

            userQuery.whereEqualTo("objectId", id);

        } catch (JSONException e) {

        }

当我运行应用程序时,它会导致空指针异常...

object = new JSONObject(jsonData);

我运行调试器并停在

userQuery.whereEqualTo("objectId", id);

Object不为null,也不是action,id或jsonData。我对此感到困惑。这是在此之后运行的下一个代码块...

userQuery.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> parseUsers, ParseException e) {
                user = parseUsers.get(0);
                if(action.equals(connectAction)){
                    if(!connectedB){
                        connectedB = true;
                            Intent broadcast = new Intent();
                            broadcast.setAction(yesAction);
                            broadcast.putExtra("extra","yes");
                            context.sendBroadcast(broadcast);

                    }

                }
            }

        });

这只是在Parse中搜索具有相同对象ID的用户。然后一旦找到一个,它就会向另一个活动中的另一个广播接收器发送广播。此后没有其他代码。这是日志猫。

          04-15 23:04:30.983  11802-11802/joke.app.randomchat E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: joke.app.randomchat, PID: 11802
java.lang.RuntimeException: Unable to start receiver joke.app.randomchat.MessageReciever: java.lang.NullPointerException
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2580)
        at android.app.ActivityThread.access$1700(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
        at org.json.JSONTokener.nextValue(JSONTokener.java:94)
        at org.json.JSONObject.<init>(JSONObject.java:155)
        at org.json.JSONObject.<init>(JSONObject.java:172)
        at joke.app.randomchat.MessageReciever.onReceive(MessageReciever.java:57)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2573)

第57行是我上面谈到的那一行。我该如何解决这个问题?

这是我在userQuery停止时的值...

Bundle[{com.parse.Data={"id":"at790nlYIN","action":"joke.app.randomchat.intent.CONNECT","push_hash":"d41d8cd98f00b204e9800998ecf8427e"}, com.parse.Channel=null}]

这是对象

{"action":"joke.app.randomchat.intent.CONNECT","id":"at790nlYIN","push_hash":"d41d8cd98f00b204e9800998ecf8427e"}

动作字符串

joke.app.randomchat.intent.CONNECT

Id String

at790nlYIN

jsonData

{"id":"at790nlYIN","action":"joke.app.randomchat.intent.CONNECT","push_hash":"d41d8cd98f00b204e9800998ecf8427e"}

2 个答案:

答案 0 :(得分:0)

您就是这样做的:

try {
    JSONObject jsonData = new JSONObject(intent.getExtras().getString("com.parse.Data"));


    action = jsonData .getString("action");
    id = jsonData .getString("id");

    userQuery.whereEqualTo("objectId", id);

} catch (JSONException e) {

}

答案 1 :(得分:0)

好的,所以我才弄明白这个问题。它不是jsonData或JSONObject。在这段代码中......

userQuery.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> parseUsers, ParseException e) {
            user = parseUsers.get(0);
            if(action.equals(connectAction)){
                if(!connectedB){
                    connectedB = true;
                        Intent broadcast = new Intent();
                        broadcast.setAction(yesAction);
                        broadcast.putExtra("extra","yes");
                        context.sendBroadcast(broadcast);

                }

            }
        }

    });

我向另一个接收者发送广播,但将动作设置为错误的动作。所以它再次称这个接收器,并且com.parse.Data中没有任何意图。一旦我改变了

broadcast.setAction(yesAction);

broadcast.setAction(receivedUpdate);

它完美无缺。