[英]Attempt to invoke virtual method boolean java.lang.String.equals(java.lang.Object) on a null object reference in android studio firebase mainactivity
I am making a chatting application in which when I login, I get this error in Logcat and my app crashes.我正在制作一个聊天应用程序,当我登录时,我在Logcat中收到此错误并且我的应用程序崩溃了。
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.我不明白为什么会出现此错误,我在我的应用程序中使用了 firebase。 My main activity is very large, so I uploaded the photo of code.我的主要活动很大,所以我上传了代码的照片。
I don't understand why I get this error.我不明白为什么会出现此错误。
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.尝试使用addListenerForSingleValueEvent(new ValueEventListener() {
而不是ValueEventListener
因为每次更改“引用”树时都会触发ValueEventListener
,如果没有任何反应,它不会被触发,并且此代码块将不会运行。
However addListenerForSingleValueEvent
runs this block of code one time no matter what.然而, addListenerForSingleValueEvent
无论如何都会运行这段代码一次。
Also check that 'refernece' is pointing to the exact place in your Firebase that you want to listen to.还要检查“refernece”是否指向您想要收听的 Firebase 中的确切位置。 And if it's right check that the user it's pointing to isn't null.如果正确,请检查它指向的用户是否不为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.