简体   繁体   English

从助手类返回时ParseObject为null

[英]ParseObject is null when returned from helper class

From my Activity I attempt to call the method that should return the Parseobject: 从我的活动中,我尝试调用应返回Parseobject的方法:

ParseQueryHelper parseQueryHelper = new ParseQueryHelper(CommentActivity.this, commentId);
ParseObject finalTopLevelCommentObject = parseQueryHelper.getTopLevelComment();
System.out.println("Watermelon: " + finalTopLevelCommentObject.getString("notificationText"));

But the ParseObject when returned from the helper class is null: 但是从帮助程序类返回的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];
    }
}

Exception: 例外:

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) 

I debugged and verified that the String commentId that is passed into the method's constructor is valid, and that the object exists in the database. 我调试并验证了传递到方法的构造函数中的String commentId是否有效,并且该对象存在于数据库中。 But the ParseObject or result[0] is empty when it is returned. 但是,ParseObject或result[0]返回时为空。 Why? 为什么?

Alternatively, but this is not Async: 或者,但这不是异步的:

// 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;
}

这是正常现象,符合预期,您的代码是异步的,在您return result[0] ,尚未用查询响应填充result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM