-1
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.shopingonline, PID: 18327
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shopingonline/com.example.shopingonline.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.shopingonline.Model.Users.getName()' on a null object reference

View headerView = navigationView.getHeaderView(0);
        TextView userNameTextView = headerView.findViewById(R.id.user_profile_name);
        CircleImageView profileImageView = headerView.findViewById(R.id.user_profile_image);

        userNameTextView.setText(Prevalent.currentOnlineUser.getName());

public class Prevalent {
    public static Users currentOnlineUser;
    //unique key for all users
    public static final String UserPhoneKey = "UserPhone";
    public static final String UserPasswordKey = "UserPassword";
}



public class Users {
    //get the name passeword, all the user information
    private String name, phone, password;

    public Users(String name, String phone, String password) {
        this.name = name;
        this.phone= phone;
        this.password = password;
    }

    public Users(){

    };

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;

    } public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

// i have this problem and i can't resolve it i tried for 4 days but it's so hard for me as beginner gjvkhkbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

  • Can you ask a more specific question, it is unclear what you are trying to fix / the intended results – Zelkins Dec 15 '21 at 00:43

1 Answers1

0

From the exception which you mentioned, it can be seen that you are accessing the method getName() on a null reference which stopped you from starting the activity.

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.shopingonline, PID: 18327 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shopingonline/com.example.shopingonline.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.shopingonline.Model.Users.getName()' on a null object reference

You need to check if you are expecting a null reference in the first place, if so you can add a null check to avoid it. See this for more info

if(Prevalent.currentOnlineUser != null)
    userNameTextView.setText(Prevalent.currentOnlineUser.getName());
Turtle
  • 667
  • 1
  • 6
  • 18