对象null引用上的Firebase onDataChange

时间:2016-03-12 03:08:00

标签: nullpointerexception firebase

我有简单的代码将组成员作为:

ValueEventListener getGroupMembersListener;

presentGroupMembers = myFirebase.child("groupMembers").child(groupID);

        presentGroupMembers.addListenerForSingleValueEvent(getGroupMembersListener);

        getGroupMembersListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot groupMemberSnapshot : dataSnapshot.getChildren()) {

                    if (!groupMemberSnapshot.getKey().equals(preference.getUserSyncID(getApplicationContext()))) {
                        Member groupMember = new Member();
                        groupMember.setUserUID(groupMemberSnapshot.getKey());
                        groupMember.setUserName(groupMemberSnapshot.getValue().toString());

                        mGroupMembers.add(groupMember);

                        Log.v("InvitingFriendsToGroups", "Group member userName is: " + groupMemberSnapshot.getValue().toString());
                        Log.v("InvitingFriendsToGroups", "Group member user ID is: " + groupMemberSnapshot.getKey().toString());
                    }
                }

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                Log.v("InvitingFriendsToGroups", "Firebase Error: " + firebaseError.toString());

            }
        };

但我不断收到错误:

03-11 21:57:56.995 9100-9100/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.android.appv2, PID: 9100
                                                 java.lang.NullPointerException: Attempt to invoke interface method 'void com.firebase.client.ValueEventListener.onDataChange(com.firebase.client.DataSnapshot)' on a null object reference
                                                     at com.firebase.client.Query$1.onDataChange(Query.java:144)
                                                     at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:56)
                                                     at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
                                                     at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                     at android.os.Looper.loop(Looper.java:145)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6840)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

我已经使用Firebase几个星期了,这个错误从未出现过,也没有出现在我更复杂的查询中。

此设置可能有什么不对?

1 个答案:

答案 0 :(得分:3)

尝试移动此代码

presentGroupMembers.addListenerForSingleValueEvent(getGroupMembersListener); 

之后
getGroupMembersListener = new ValueEventListener() {

阻止,检查它是否有效,让我知道它现在是否有效。