Part 1- I have a method readData() which takes an argument of an Interface FirebaseCallBack. I am calling readData() within getAdminInfo() method and the Log.d statement (8th line) isn't getting executed. And my application is crashing with null pointer exception.
Part 2- Also firebaseCallback.Callback(newVolunteer) is throwing error "local variable is accessed from within inner class; needs to be declared final". But on this video of stack overflow user @Alex_mamo it didn't show any error. Just an intuition that I did something wrong.
public String getAdminInfo(){
dbRef = FirebaseDatabase.getInstance().getReference("Volunteer");
uid= mAuth.getCurrentUser().getUid();
readData(new FirebaseCallBack() {
@Override
public void Callback(Volunteer data) {
Log.d("crashfix" ," I am not executing ");
adminStatus = data.isAdmin();
}
});
return adminStatus;
}
//Read data from firebase
private void readData(FirebaseCallBack firebaseCallBack){
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
newVolunteer.setFullName((String) dataSnapshot.child("fullName").getValue());
newVolunteer.setAdmin((String) dataSnapshot.child("admin").getValue());
newVolunteer.setEmail((String) dataSnapshot.child("email").getValue());
newVolunteer.setGender((String) dataSnapshot.child("gender").getValue());
Log.d("crashfix" ," name fetch: " + newVolunteer.getFullName());
firebaseCallBack.Callback(newVolunteer);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
dbRef.child(uid).addListenerForSingleValueEvent(valueEventListener);
}
//Wait till the data is downloaded from firebase
private interface FirebaseCallBack{
void Callback(Volunteer data);
}
at this line I am getting null pointer exception:-
String admin = getAdminInfo();
Log.d("crashfix" ,"inside onCreate admin" + admin);
//checking if current user is admin
if(admin.equals("true")){
Toast.makeText(this,"Welcome! " + newVolunteer.getFullName() , Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
finish();
}
Crash Log:-
2020-03-30 13:41:53.060 13021-13021/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.helpinghandsorg.helpinghands, PID: 13021
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.helpinghandsorg.helpinghands/com.helpinghandsorg.helpinghands.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3123)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3266)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1957)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7099)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.helpinghandsorg.helpinghands.MainActivity.onCreate(MainActivity.java:57)
at android.app.Activity.performCreate(Activity.java:7327)
at android.app.Activity.performCreate(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3103)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3266)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1957)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7099)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)