解析异常错误消息

时间:2015-07-07 15:31:19

标签: java android parse-platform

我正在使用Parse.com作为我的后端。我试图更新用户的呼号,但Parse不断抛出异常。当试图读取消息是什么时,我收到异常错误消息“空指针。”

错误消息

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String com.parse.ParseException.getMessage()'             在Logic.BusinessLogic $ 8.done(BusinessLogic.java:303)             在Logic.BusinessLogic $ 8.done(BusinessLogic.java:293)             at com.parse.ParseTaskUtils $ 1.done(ParseTaskUtils.java:66)             在com.parse.ParseTaskUtils $ 1.done(ParseTaskUtils.java:63)             在com.parse.ParseTaskUtils $ 2 $ 1.run(ParseTaskUtils.java:107)             在android.os.Handler.handleCallback(Handler.java:739)             在android.os.Handler.dispatchMessage(Handler.java:95)             在android.os.Looper.loop(Looper.java:145)             在android.app.ActivityThread.main(ActivityThread.java:5835)             at java.lang.reflect.Method.invoke(Native Method)             在java.lang.reflect.Method.invoke(Method.java:372)

CODE

public static void UpdateCallSign(String newCallSign, final Context cntx) {

    ParseUser user = ParseUser.getCurrentUser();
    user.put("CallSign", newCallSign);
    user.saveInBackground(new SaveCallback() {
        public void done(ParseException e) {
            if (e != null) {

                Toast.makeText(cntx, "Call Sign Updated",
                        Toast.LENGTH_LONG).show();
  Log.d("Success", "Success");
                // Saved successfully
            } else {
                // ParseException

                    Log.d("Exception: ", e.getMessage());

            }
        }
    });

}

1 个答案:

答案 0 :(得分:2)

看起来你向后收到了if声明。根据你在代码中的评论,我认为你想要这个:

public static void UpdateCallSign(String newCallSign, final Context cntx) {

    ParseUser user = ParseUser.getCurrentUser();
    user.put("CallSign", newCallSign);
    user.saveInBackground(new SaveCallback() {
        public void done(ParseException e) {
            if (e == null) { <--------- This is what I changed
                // Saved successfully
                Toast.makeText(cntx, "Call Sign Updated",
                        Toast.LENGTH_LONG).show();
                Log.d("Success", "Success");
            } else {
                // ParseException
                Log.d("Exception: ", e.getMessage());
            }
        }
    });

}

因此,将if (e != null)更改为if (e == null),您应该做得很好。该错误消息告诉您,当您致电e.getMessage()时,enull。在原始代码中,当然是这种情况,因为else子句仅在e == null时触发。