1

A beginner if Java Language, my goal is when the user is login the TextView will change to the user's name but when I run the app it crashes

2021-10-06 20:01:48.389 13022-13022/com.example.chocolaterepublic E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chocolaterepublic, PID: 13022
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chocolaterepublic/com.example.chocolaterepublic.ShopMenu}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:203)
    at android.app.ActivityThread.main(ActivityThread.java:6251)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1088)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:949)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
    at com.example.chocolaterepublic.ShopMenu.onCreate(ShopMenu.java:80)
    at android.app.Activity.performCreate(Activity.java:6666)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 
    at android.os.Handler.dispatchMessage(Handler.java:110) 
    at android.os.Looper.loop(Looper.java:203) 
    at android.app.ActivityThread.main(ActivityThread.java:6251) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1088) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:949) 

the app crash before starting how do I solve this?

public class ShopMenu extends AppCompatActivity {


private FirebaseUser user;
private DatabaseReference reference;
private FirebaseAuth mAuth;

private String userID;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shop_menu);

   

    mAuth = FirebaseAuth.getInstance();
    user = FirebaseAuth.getInstance().getCurrentUser();
    reference = FirebaseDatabase.getInstance().getReference("users");
    userID = user.getUid();

   final TextView guestUser = (TextView) findViewById(R.id.guest_User);

    reference.child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            UserModel userProfile = snapshot.getValue(UserModel.class);

            if (userProfile != null){
                String fullName = userProfile.getName();

                guestUser.setText(fullName);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

            Toast.makeText(ShopMenu.this, "Error: Something Happen", Toast.LENGTH_SHORT).show();

        }
    });
  • Are you sure you are authenticated when using the `getUid()` method? Please respond with @AlexMamo – Alex Mamo Oct 06 '21 at 12:33
  • @AlexMamo yes the user is authenticated when run the app – James gonzales Oct 06 '21 at 12:51
  • How do you check that? – Alex Mamo Oct 06 '21 at 13:09
  • @AlexMamo when the user is sign-In and I go the Account Activity I go there and if it's sing-Out I go to the Login Activity, I temporary remove the codes that causing the error to see if the authentication – James gonzales Oct 06 '21 at 13:15
  • 1
    Your FirebaseUser object is null, otherwise you won't get that message, right? – Alex Mamo Oct 06 '21 at 13:16
  • If you set a breakpoint on the line `userID = user.getUid();` and run in the debugger, you will find that `user` is null. Simillarly: if you put `if (user == null) throw new Exception("No active user");` on the line right above that, you will see that this error is raised. – Frank van Puffelen Oct 06 '21 at 14:05

0 Answers0