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.
I don't understand why I get this error.
CodePudding user response:
Try using "addListenerForSingleValueEvent(new ValueEventListener() {" instead of "ValueEventListener" Because ValueEvetListener 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 thagt you won't to listen to. And if it's right check that the user it's poiniting to isn't null.