从我的Activity中我尝试调用应该返回Parseobject的方法:
ParseQueryHelper parseQueryHelper = new ParseQueryHelper(CommentActivity.this, commentId);
ParseObject finalTopLevelCommentObject = parseQueryHelper.getTopLevelComment();
System.out.println("Watermelon: " + finalTopLevelCommentObject.getString("notificationText"));
但是从helper类返回的ParseObject为null:
class ParseQueryHelper {
private Context mContext;
private String mCommentId;
ParseQueryHelper(Context context, String commentId) {
this.mContext = context;
this.mCommentId = commentId;
}
// Retrieve the top-level Comment in a conversation thread
ParseObject getTopLevelComment() {
final ParseObject[] result = {null};
ParseQuery<ParseObject> query = new ParseQuery<>(ParseConstants.CLASS_YEET);
query.whereContains(ParseConstants.KEY_OBJECT_ID, mCommentId);
query.findInBackground((yeets, e) -> {
if (e == null) {
for (ParseObject topLevelCommentObject : yeets) {
if (topLevelCommentObject != null) {
result[0] = topLevelCommentObject;
}
}
}
});
return result[0];
}
}
例外:
08 - 28 01: 56: 57.156 22015 - 22015 / com.test.android E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.test.android, PID: 22015
java.lang.RuntimeException: Unable to start activity ComponentInfo {
com.test.android / com.test.android.activity.CommentActivity
}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseObject.getString(java.lang.String)'
on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2547)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2613)
at android.app.ActivityThread.access$900(ActivityThread.java: 180)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1473)
at android.os.Handler.dispatchMessage(Handler.java: 111)
at android.os.Looper.loop(Looper.java: 207)
at android.app.ActivityThread.main(ActivityThread.java: 5710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 761)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseObject.getString(java.lang.String)'
on a null object reference
at com.test.android.activity.CommentActivity.createTopLevelCommentObject(CommentActivity.java: 198)
at com.test.android.activity.CommentActivity.onCreate(CommentActivity.java: 115)
at android.app.Activity.performCreate(Activity.java: 6288)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2500)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2613)
at android.app.ActivityThread.access$900(ActivityThread.java: 180)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1473)
at android.os.Handler.dispatchMessage(Handler.java: 111)
at android.os.Looper.loop(Looper.java: 207)
at android.app.ActivityThread.main(ActivityThread.java: 5710)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 900)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 761)
我调试并验证了传递给方法构造函数的String commentId是否有效,以及该对象是否存在于数据库中。但是当返回时,ParseObject或result[0]
为空。为什么呢?
或者,这不是异步:
// Retrieve the top-level Comment in a conversation thread
ParseObject getTopLevelComment() {
ParseObject result = null;
ParseQuery<ParseObject> query = new ParseQuery<>(ParseConstants.CLASS_YEET);
query.whereContains(ParseConstants.KEY_OBJECT_ID, mCommentId);
try {
List<ParseObject> results = query.find();
result = results.get(0);
return result;
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
答案 0 :(得分:0)
这是正常的,预期的,您的代码是异步的,当时return result[0]
,result
尚未填充查询响应。