-2

So a bit of a heads up, I build a small Mobile Chat App for my task, everything works fine until I get NullPointerException when I do transition from my Contact List menu to the Chat menu... In the error I got this

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.bruh.chatapp, PID: 3797 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bruh.chatapp/com.bruh.chatapp.Obrol}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2193) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5019) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.bruh.chatapp.Obrol.onCreate(Obrol.java:60) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2243)  at android.app.ActivityThread.access$800(ActivityThread.java:135)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5019)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  at dalvik.system.NativeStart.main(Native Method) 

As you can see it's caused by a line in Obrol.java at line 60 which is containing

final DatabaseReference reference1 = firebaseDatabase.getReference("https://databaselink/Message/" + Pesan.Nama+"_"+Pesan.chatWith);

I also have tried change getReference() to getReferencefromUrl() but it stills returns NullPointerException

Then I tried to just make it as a comment to test if the reference1 object did causes this error, and it is, when I relaunch my app, and do transition from my Contact List menu to the Chat menu, it doesn't crash the app and also doesn't return NullPointerException error...

So I wonder why is this happening and how do I solve this error? Oh, here's my Firebase Database structure for additional reference

Database Structure

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
sterben
  • 59
  • 1
  • 8

2 Answers2

1

Change this:

final DatabaseReference reference1 = firebaseDatabase.getReference("https://databaselink/Message/" + Pesan.Nama+"_"+Pesan.chatWith);

into this:

final DatabaseReference reference1 = firebaseDatabase.getReference().child("Users");

You don't have to add the URL of the database, the getReference() will return the root node of the database.

Peter Haddad
  • 65,099
  • 21
  • 108
  • 107
0

The reason you got this error is because your reference1 variable is returning null.

As for the cause, there are several possibilities (these are what I've encountered):

  1. The node you referenced doesn't exist, so make sure that the node either root or child you referenced is exist in your database

  2. You forgot to get Instance of your database, so make sure you indeed have getInstance in your Class before trying to get DatabaseReference to your class. To do getInstance you can use this code

final FirebaseDatabase database = FirebaseDatabase.getInstance();

reference: https://firebase.google.com/docs/database/admin/retrieve-data

sterben
  • 59
  • 1
  • 8