So I'm trying to create a multilevel user login page for Android Studio, I have a working basic login. I added user levels to the database but my application keeps crashing. I'm using Android Studio and Firebase as backend.
Login.Class
private void checkUserType() {
DatabaseReference databaseReference = firebaseDatabase.getReference().child("Users").child(firebaseAuth.getUid());
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
int userType = (userProfile.getUsertype());
switch (userType) {
case 0:
startActivity(new Intent(Login.this, DoctorActivity.class));
break;
case 1:
startActivity(new Intent(Login.this, MainActivity.class));
break;
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});
}
This is what my database looks like:
Logcat:
Process: com.example.trixiavillarama.hope, PID: 5524 java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.FirebaseDatabase.getReference()' on a null object reference at com.example.trixiavillarama.hope.Login.checkUserType(Login.java:124) at com.example.trixiavillarama.hope.Login.checkEmailVerification(Login.java:116) at com.example.trixiavillarama.hope.Login.access$300(Login.java:26) at com.example.trixiavillarama.hope.Login$4.onComplete(Login.java:91) at com.google.android.gms.tasks.zzj.run(Unknown Source:23) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(Zyg oteInit.java:858)
You are getting the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.FirebaseDatabase.getReference()' on a null object reference
Because your firebaseDatabase
object is null
, you forgot to initialize it. To solve this, please add the following line of code:
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
Right before the following line of code:
DatabaseReference databaseReference = firebaseDatabase.getReference().child("Users").child(firebaseAuth.getUid());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.