I can’t find solution for "java.lang.Nullpointerexception" Error - Android Studio

Asked

Viewed 790 times

1

The following error appears:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
                  at novapetbeta.platypus.com.novapetbeta.Principal$6.onChildAdded(Principal.java:185)
                  at com.google.android.gms.internal.zzdri.zza(Unknown Source:33)
                  at com.google.android.gms.internal.zzdwu.zzbvb(Unknown Source:2)
                  at com.google.android.gms.internal.zzdxa.run(Unknown Source:65)
                  at android.os.Handler.handleCallback(Handler.java:790)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6494)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

This is the code that is showing the error.

public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if(dataSnapshot.exists() && !dataSnapshot.child("connections").child("Meh").hasChild(atualUID) && !dataSnapshot.child("connections").child("Gostei").hasChild(atualUID)){
                String ImagemPerfilURL = "default";
                if (!dataSnapshot.child("ImagemPerfilURL").getValue().equals("default")){
                    ImagemPerfilURL = dataSnapshot.child("ImagemPerfilURL").getValue().toString();
                }

I’m new to mobile development and I’m not getting around to it. Thanks for your help.

  • 1

    Observing the error message we see that Attempt to invoke virtual method '...equals...' on a null object reference => "Tried to execute equals about something that is null". It certainly refers to this line dataSnapshot.child("ImagemPerfilURL").getValue().equals("default") in which dataSnapshot.child("ImagemPerfilURL").getValue() must be bringing null. Start there, trying to understand what this part gives as value and why. Can and should always do Debug the application and use the Logcat to try to extract as much information as possible and understand what values are in each part of the code

  • 1

    NullPointerException it is because dataSnapshot.child("ImagemPerfilURL").getValue() is null or no empty, you are making an equals of an empty variable, check the JSON path, it can be this

1 answer

1

You used the method equals when the dataSnapshot.child("ImagemPerfilURL").getValue() is void.

If Child "ImagemPerfilURL" cannot be null, then that is the real problem, being the NullPointerException only result of this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.