我正在使用Parse SDK for Android。我遇到了这个问题,当我试图保存ParseObject时,它抛出一个空指针异常,但是这个对象不是null。在此之前,它工作正常。我正在改变并添加其他东西,但我没有触及这个,现在它失败了。 我有Lighter类扩展ParseObject。我试图在后台保存它:
lighterObject.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
message.setContent(enteredMessage);
message.setLocation(lighterObject.getParseGeoPoint("location"));
message.setUser(ParseObject.createWithoutData(user.getClassName(), user.getObjectId()));
message.setIsScanMessage("YES");
message.setType(1d);
message.setPublicReadWriteAcl();
user.put("lastLighter", ParseObject.createWithoutData(lighterObject.getClassName(), lighterObject.getObjectId()));
user.saveInBackground();
message.setLighter(ParseObject.createWithoutData(lighterObject.getClassName(), lighterObject.getObjectId()));
message.saveInBackground();
AppDelegate.getSharedPreferences().edit().putString(AppDelegate.LIGHTER_ID, lighterObject.getObjectId()).commit();
AppDelegate.getSharedPreferences().edit().putString(AppDelegate.LIGHTER_COLOR, color.toLowerCase()).commit();
progressBar.dismiss();
fragmentHolder.onMapRedirect();
} else {
progressBar.dismiss();
fragmentHolder.onMapRedirect();
e.printStackTrace();
Toast.makeText(getActivity(), "Failed to register a lighter", Toast.LENGTH_SHORT).show();
}
}
});
我调试了代码,我可以肯定地说,lightObject不是null。在调试期间,程序转到else语句,但错误表明我试图在null对象上使用getObjectId()
方法。这是日志:
W/System.err﹕ com.parse.ParseException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseUser.getObjectId()' on a null object reference
W/System.err﹕ at com.parse.Parse$6$1.run(Parse.java:943)
W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err﹕ at android.os.Looper.loop(Looper.java:211)
W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5317)
W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
W/System.err﹕ Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseUser.getObjectId()' on a null object reference
W/System.err﹕ at com.parse.ParseUser.validateSave(ParseUser.java:582)
W/System.err﹕ at com.parse.ParseObject$40$2$3$1$1.then(ParseObject.java:2350)
W/System.err﹕ at com.parse.ParseObject$40$2$3$1$1.then(ParseObject.java:2342)
W/System.err﹕ at bolts.Task$14.run(Task.java:796)
W/System.err﹕ at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
W/System.err﹕ at bolts.Task.completeAfterTask(Task.java:787)
W/System.err﹕ at bolts.Task.access$200(Task.java:31)
W/System.err﹕ at bolts.Task$10.then(Task.java:592)
W/System.err﹕ at bolts.Task$10.then(Task.java:589)
W/System.err﹕ at bolts.Task.runContinuations(Task.java:832)
W/System.err﹕ at bolts.Task.access$600(Task.java:31)
W/System.err﹕ at bolts.Task$TaskCompletionSource.trySetResult(Task.java:887)
W/System.err﹕ at bolts.Task$TaskCompletionSource.setResult(Task.java:921)
W/System.err﹕ at com.parse.ParseObject$3.then(ParseObject.java:401)
W/System.err﹕ at com.parse.ParseObject$3.then(ParseObject.java:398)
W/System.err﹕ at bolts.Task$13.run(Task.java:755)
W/System.err﹕ at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
W/System.err﹕ at bolts.Task.completeImmediately(Task.java:746)
W/System.err﹕ at bolts.Task.continueWith(Task.java:545)
W/System.err﹕ at bolts.Task.continueWith(Task.java:556)
W/System.err﹕ at com.parse.ParseObject.enqueueForAll(ParseObject.java:398)
W/System.err﹕ at com.parse.ParseObject$40$2$3.then(ParseObject.java:2339)
W/System.err﹕ at com.parse.ParseObject$40$2$3.then(ParseObject.java:2328)
W/System.err﹕ at bolts.Task$14.run(Task.java:796)
W/System.err﹕ at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
W/System.err﹕ at bolts.Task.completeAfterTask(Task.java:787)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:599)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:610)
W/System.err﹕ at bolts.Task$12.then(Task.java:702)
W/System.err﹕ at bolts.Task$12.then(Task.java:690)
W/System.err﹕ at bolts.Task$14.run(Task.java:796)
W/System.err﹕ at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
W/System.err﹕ at bolts.Task.completeAfterTask(Task.java:787)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:599)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:574)
W/System.err﹕ at bolts.Task.onSuccessTask(Task.java:690)
W/System.err﹕ at bolts.Task.onSuccessTask(Task.java:680)
W/System.err﹕ at bolts.Task.onSuccessTask(Task.java:714)
W/System.err﹕ at com.parse.ParseObject$40$2.then(ParseObject.java:2328)
W/System.err﹕ at com.parse.ParseObject$40$2.then(ParseObject.java:2279)
W/System.err﹕ at bolts.Task$14.run(Task.java:796)
W/System.err﹕ at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
W/System.err﹕ at bolts.Task.completeAfterTask(Task.java:787)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:599)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:610)
W/System.err﹕ at bolts.Task$12.then(Task.java:702)
W/System.err﹕ at bolts.Task$12.then(Task.java:690)
W/System.err﹕ at bolts.Task$14.run(Task.java:796)
W/System.err﹕ at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
W/System.err﹕ at bolts.Task.completeAfterTask(Task.java:787)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:599)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:574)
W/System.err﹕ at bolts.Task.onSuccessTask(Task.java:690)
W/System.err﹕ at bolts.Task.onSuccessTask(Task.java:680)
W/System.err﹕ at bolts.Task$8.then(Task.java:503)
W/System.err﹕ at bolts.Task$8.then(Task.java:495)
W/System.err﹕ at bolts.Task$14.run(Task.java:796)
W/System.err﹕ at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
W/System.err﹕ at bolts.Task.completeAfterTask(Task.java:787)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:599)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:574)
W/System.err﹕ at bolts.Task.continueWhile(Task.java:509)
W/System.err﹕ at bolts.Task.continueWhile(Task.java:465)
W/System.err﹕ at com.parse.ParseObject$40.then(ParseObject.java:2274)
W/System.err﹕ at com.parse.ParseObject$40.then(ParseObject.java:2263)
W/System.err﹕ at bolts.Task$14.run(Task.java:796)
W/System.err﹕ at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
W/System.err﹕ at bolts.Task.completeAfterTask(Task.java:787)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:599)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:610)
W/System.err﹕ at bolts.Task$12.then(Task.java:702)
W/System.err﹕ at bolts.Task$12.then(Task.java:690)
W/System.err﹕ at bolts.Task$14.run(Task.java:796)
W/System.err﹕ at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
W/System.err﹕ at bolts.Task.completeAfterTask(Task.java:787)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:599)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:574)
W/System.err﹕ at bolts.Task.onSuccessTask(Task.java:690)
W/System.err﹕ at bolts.Task.onSuccessTask(Task.java:680)
W/System.err﹕ at bolts.Task.onSuccessTask(Task.java:714)
W/System.err﹕ at com.parse.ParseObject.deepSaveAsync(ParseObject.java:2263)
W/System.err﹕ at com.parse.ParseObject.access$400(ParseObject.java:52)
W/System.err﹕ at com.parse.ParseObject$13.then(ParseObject.java:1403)
W/System.err﹕ at com.parse.ParseObject$13.then(ParseObject.java:1393)
W/System.err﹕ at bolts.Task$14.run(Task.java:796)
W/System.err﹕ at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
W/System.err﹕ at bolts.Task.completeAfterTask(Task.java:787)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:599)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:610)
W/System.err﹕ at bolts.Task$12.then(Task.java:702)
W/System.err﹕ at bolts.Task$12.then(Task.java:690)
W/System.err﹕ at bolts.Task$14.run(Task.java:796)
W/System.err﹕ at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105)
W/System.err﹕ at bolts.Task.completeAfterTask(Task.java:787)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:599)
W/System.err﹕ at bolts.Task.continueWithTask(Task.java:574)
W/System.err﹕ at bolts.Task.onSuccessTask(Task.java:690)
W/System.err﹕ at bolts.Task.onSuccessTask(Task.java:680)
W/System.err﹕ at bolts.Task.onSuccessTask(Task.java:714)
W/System.err﹕ at com.parse.ParseObject.saveAsync(ParseObject.java:1371)
W/System.err﹕ at com.parse.ParseObject$10.then(ParseObject.java:1358)
W/System.err﹕ at com.parse.ParseObject$10.then(ParseObject.java:1354)
W/System.err﹕ at com.parse.TaskQueue.enqueue(TaskQueue.java:61)
W/System.err﹕ at com.parse.ParseObject.saveInBackground(ParseObject.java:1354)
W/System.err﹕ at com.parse.ParseObject.saveInBackground(ParseObject.java:1442)
W/System.err﹕ at ergotech.com.cricket.Fragments.FragmentLighterNewOwner$2.onClick(FragmentLighterNewOwner.java:139)
W/System.err﹕ at android.view.View.performClick(View.java:4832)
W/System.err﹕ at android.view.View$PerformClick.run(View.java:19839)
答案 0 :(得分:0)
你是否声称lightObject为final?
https://parse.com/questions/how-to-get-the-parse-object-id-after-saving-it-with-saveinbackgroud