0

When I run the app and go to the user's activity the app crashes showing me that the mUsersList.setHasFixedSize(true); is making the app crash.

this is the message "Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference"

private RecyclerView mUsersList;
private DatabaseReference mUsersDatabase;

@Override
protected void onCreate( Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.users_single_layout);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");




    mUsersList = findViewById(R.id.users_list);
    mUsersList.setHasFixedSize(true);
    mUsersList.setLayoutManager(new LinearLayoutManager(this));

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mango orange
  • 118
  • 9
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Bö macht Blau Nov 09 '19 at 17:18
  • The layout users_single_layout that you inflate does not contain a view with id: `users_list`. – forpas Nov 09 '19 at 17:21

1 Answers1

1

The stacktrace tells you everything you need. mUsersList is null, so you can't call any methods on it. You should make sure your layout file R.layout.users_single_layout has a RecyclerView with id of "@+id/users_list" defined in it. Also, you should do a null pointer check:

mUsersList = findViewById(R.id.users_list);
if (mUsersList != null) {
    mUsersList.setHasFixedSize(true);
    mUsersList.setLayoutManager(new LinearLayoutManager(this));
}
Chris
  • 1,180
  • 1
  • 9
  • 17