-2

I am trying to load the active users information into a new activity and display it. I have done similar things in different activities and they have worked fine, but for some reason this is not and I am not sure. Whenever android studio tries to invoke a method on user, it throws a NullPointerException.

I have tried everything I can think of, reformatting the way its written, nesting in different methods, passing data through to it in different ways. Nothing is working and every article I've found on the subject doesnt help at all

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_edit);
    int userID = getIntent().getIntExtra("sessionUser", 0);
    tuckBoxDao = TuckBoxDB.createTuckBoxDB(this).tbDao();

    Email = findViewById(R.id.email);
    Username = findViewById(R.id.username);
    Password = findViewById(R.id.password);
    Mobile = findViewById(R.id.phoneNumber);
    Notifications = findViewById(R.id.notificationBox);
    Emails = findViewById(R.id.emailBox);
    registerButton = findViewById(R.id.registerButton);
    registerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            registerUser(user);
        }
    });

    backButton = findViewById(R.id.backButton);
    backButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            goBack();
        }
    });

    user = LoginActivity.tuckBoxDB.tbDao().searchById(userID);

    String username = user.getUsername();
    Username.setText(username);
    String email = user.getEmail();
    Email.setHint(email);
    String mobile = user.getMobile();
    Mobile.setHint(mobile);
    Boolean notifications = user.getNotifications();
    Notifications.setChecked(notifications);
    Boolean emails = user.getEmails();
    Emails.setChecked(emails);

}

I would have expected this to work fine and update and display the right information, as it has done exactly that in a fragment I made earlier, but for some reason it is throwing as soon as it gets to user.getUsername();

3 Answers3

0

user is null. Check null like below

if(user != null){
String username = user.getUsername();
Username.setText(username);
String email = user.getEmail();
Email.setHint(email);
String mobile = user.getMobile();
Mobile.setHint(mobile);
Boolean notifications = user.getNotifications();
Notifications.setChecked(notifications);
Boolean emails = user.getEmails();
Emails.setChecked(emails);
}
Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22
0

when you see this error defiantly user is null so there is some things you have to check it in debug mode:

1.maybe this function has some bugs:

LoginActivity.tuckBoxDB.tbDao().searchById(userID);
  1. maybe this function can't find int extra. check start activity do you pass this extra and check does your key(sessionUser) is equal in both of them:

    getIntent().getIntExtra("sessionUser", 0)

Amir Abbas
  • 305
  • 3
  • 8
0

NullPointerException cause is if you are invoking any method on null object then definitely it is going to throw this exception.

It is incorrect coding practice to directly invoke a method on an object which you are getting at runtime unless you are 100% sure that there will be some object even if empty. So, only NullPointerException is unchecked exception.

Coming to your question,

Please confirm that you are getting proper userId and is that userId present in db.

searchById(userId),

is not able to locate the object with given userId so it is returning instead. You just need to do null check before invoking method on user

if(user!=null)

and perform operations if value is above condition is true but this is subjective to what is your business requirement. Above condition will just save you from this exception, but if in any case you need user object, then check for userId that you are getting and its presence in db.

You are retrivi