如何解决“尝试在空对象引用上调用虚拟方法'java.lang.String java.lang.Object.toString()'”?

时间:2019-11-01 06:36:45

标签: java android firebase

我正在尝试解决过去24小时以来的空指针异常问题。它在getOppositeSexUsers()中不断显示错误。我问了一些问题,也得到了答复,但它显示了空指针异常的传递时间。

这是logcat。

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at com.techjd.hubu.MainActivity$6.onChildAdded(MainActivity.java:205)
        at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.0:79)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.0:55)
        at android.os.Handler.handleCallback(Handler.java:815)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5740)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)

这是主要的活动代码。


    public void getOppositeSexUsers(){
        DatabaseReference oppositeSexDb = FirebaseDatabase.getInstance().getReference().child("Users").child(oppositeUserSex);
        oppositeSexDb.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, String s) {
                if (dataSnapshot.exists() && !dataSnapshot.child("connections").child("nope").hasChild(currentUId) && !dataSnapshot.child("connections").child("yes").hasChild(currentUId)){
                    Object ImageData = dataSnapshot.child("profileImageUrl").getValue();
                    String ImageDefault = "default";

                    if (ImageData != null){
                        ImageDefault = ImageData.toString();
                    }



                    cards item = new cards(dataSnapshot.getKey(), dataSnapshot.child("Name").getValue().toString() , ImageDefault);


                    //cards item = new cards(dataSnapshot.getKey(), dataSnapshot.child("Name").getValue().toString(), dataSnapshot.child("profileImageUrl").getValue().toString());
                    rowItems.add(item);
                    arrayAdapter.notifyDataSetChanged();
                }
            }
            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, String s) {
            }
            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, String s) {
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });

    }

每次我在getOppositeSexUsers()中尝试一个解决方案时,它将生成Null Pointer Exception。什么是修复?

1 个答案:

答案 0 :(得分:0)

要避免这种情况,exception就像在null中一样,请检查Name Data中的ImageData

 @Override
 public void onChildAdded(@NonNull DataSnapshot dataSnapshot, String s) {
        if (dataSnapshot.exists() && !dataSnapshot.child("connections").child("nope").hasChild(currentUId) && !dataSnapshot.child("connections").child("yes").hasChild(currentUId)){
             Object ImageData = dataSnapshot.child("profileImageUrl").getValue();
             String ImageDefault = "default";

             if (ImageData != null){
                        ImageDefault = ImageData.toString();
             }

             // here check your name properity is null or not like below
             String nameDataDefault = "default";
             Object nameData = dataSnapshot.child("Name").getValue(); // check key is Name or name.

             if (nameData != null){
                  nameDataDefault = nameData.toString();
             }
             cards item = new cards(dataSnapshot.getKey(), nameDataDefault, ImageDefault);


                    //cards item = new cards(dataSnapshot.getKey(), dataSnapshot.child("Name").getValue().toString(), dataSnapshot.child("profileImageUrl").getValue().toString());
                    rowItems.add(item);
                    arrayAdapter.notifyDataSetChanged();
                }
            }