0

I am making a chatting application in which when I login, I get this error in Logcat and my app crashes.

2022-03-12 16:09:44.805 22115-22115/com.dccodes.chugli E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.dccodes.chugli, PID: 22115
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at com.dccodes.chugli.MainActivity$2.onDataChange(MainActivity.java:91)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
        at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6810)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

I don't understand why I get this error, I used firebase for my app. My main activity is very large, so I uploaded the photo of code.

enter image description here enter image description here

I don't understand why I get this error.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
KING
  • 55
  • 9
  • 3
    Please edit your question and add the code as text and **not** as an image. – Alex Mamo Mar 12 '22 at 11:15
  • 1
    Besides that, please edit your question and add your database structure as a JSON file. You can simply get it by clicking the Export JSON in the overflow menu (⠇) in your [Firebase Console](https://console.firebase.google.com/u/0/project/_/database/data). – Alex Mamo Mar 12 '22 at 11:16

1 Answers1

0

Try using addListenerForSingleValueEvent(new ValueEventListener() { instead of ValueEventListener Because ValueEventListener is triggered everytime that you have a change in 'reference' tree, if nothing happens it won't be triggered, and this block of code won't run.

However addListenerForSingleValueEvent runs this block of code one time no matter what.

Also check that 'refernece' is pointing to the exact place in your Firebase that you want to listen to. And if it's right check that the user it's pointing to isn't null.

Boken
  • 4,825
  • 10
  • 32
  • 42
Omer
  • 51
  • 5