无法解决我的toString引发的NullPointerException

时间:2019-08-02 15:14:52

标签: java android

我正在尝试检查Firebase控制台中用户的数据是否为空或不是空字符串。为此,我使用DataSnapshot。当我尝试使用dataSnapshot.getValue().toString() my toString()时会抛出NullPointerException。在这种情况下,我不确定如何解决此问题。请帮忙。

public void checkIfUserDataExists(){

        if(mAuth.getCurrentUser() != null) {

            userID = mAuth.getCurrentUser().getUid();
            myRef = FirebaseDatabase.getInstance().getReference().child("user_profile_info").child(userID);

            myRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    Log.i("snapshot data", dataSnapshot.getValue().toString());
                    if (!dataSnapshot.getValue().toString().isEmpty() && !dataSnapshot.getValue().toString().equals("")) {

                        Intent intent = new Intent(SignInActivity.this, MenuActivity.class);
                        startActivity(intent);

                    } else {

                        Intent intent = new Intent(SignInActivity.this, ProfileActivity.class);
                        startActivity(intent);

                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    }

2019-08-02 16:01:31.581 28329-28329/com.example.seansandroid.dementiaapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.seansandroid.dementiaapp, PID: 28329
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at com.example.seansandroid.dementiaapp.Login.SignInActivity$2.onDataChange(SignInActivity.java:137)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

发生这种情况是因为我从数据库中删除了一些输入数据以尝试检查某些内容。现在,由于dataSnapshot中没有数据,因此toString()抛出了NullPointerException。无论如何,我有没有要把这个指向空字符串而不是null?

2 个答案:

答案 0 :(得分:2)

您可以测试非null dataSnapshot.getValue()

public void checkIfUserDataExists(){

        if(mAuth.getCurrentUser() != null) {

            userID = mAuth.getCurrentUser().getUid();
            myRef = FirebaseDatabase.getInstance().getReference().child("user_profile_info").child(userID);

            myRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    Log.i("snapshot data", "dataSnapshot.getValue() = " + dataSnapshot.getValue());

                    if ((dataSnapshot.getValue() != null) && !dataSnapshot.getValue().toString().isEmpty() && !dataSnapshot.getValue().toString().equals("")) {

                        Intent intent = new Intent(SignInActivity.this, MenuActivity.class);
                        startActivity(intent);

                    } else {

                        Intent intent = new Intent(SignInActivity.this, ProfileActivity.class);
                        startActivity(intent);

                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    }

答案 1 :(得分:1)

您可以使用Firebase的exits()方法来检查数据快照是否退出。

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

    if (dataSnapshot.exists()) {
        Log.i("snapshot data", "dataSnapshot.getValue() = " + dataSnapshot.getValue());
        Intent intent = new Intent(SignInActivity.this, MenuActivity.class);
        startActivity(intent);

    } else {

        Intent intent = new Intent(SignInActivity.this, ProfileActivity.class);
        startActivity(intent);

    }
}