Why am I getting NullPointerException on setHasFixedSize(true) [I mentioned the lined in the code] in Android Studio?-ThrowExceptions

Exception or error:

Why I am getting an NullPointerException error for setHasFixedSize(true)?
I am unable to understand this:
My code is:

public class SearchFragment extends Fragment {

private RecyclerView recyclerView;
private UserAdapter userAdapter;
private List<User> mUsers;

EditText search_bar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_search, container, false);

    recyclerView=view.findViewById(R.id.recycler_view);

   //Below line
   recyclerView.setHasFixedSize(true);///////////////////////////AT THIS LINE I AM GETTNG ERROR.WHY?
   //Upper line  

  recyclerView.setLayoutManager(new LinearLayoutManager((getContext())));

    search_bar=view.findViewById(R.id.search_bar);

    mUsers= new ArrayList<>();
    userAdapter = new UserAdapter(getContext(), mUsers);
    recyclerView.setAdapter(userAdapter);

    readUsers();
    search_bar.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                searchUsers(charSequence.toString().toLowerCase());
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    return view;

}

private void searchUsers(String s){
    Query query = FirebaseDatabase.getInstance().getReference("Users").orderByChild("username")
            .startAt(s)
            .endAt(s+"\uf8ff");

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mUsers.clear();
                for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                    User user = snapshot.getValue(User.class);
                    mUsers.add(user);
                }

                userAdapter.notifyDataSetChanged();
            }

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

        }
    });
}

private void readUsers(){

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(search_bar.getText().toString().equals("")){
                mUsers.clear();
                for(DataSnapshot snapshot: dataSnapshot.getChildren()){
                    User user = snapshot.getValue(User.class);
                    mUsers.add(user);
                }

                userAdapter.notifyDataSetChanged();
            }
        }

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

        }
    });
}

}

In this fragment code I am trying to understand that why there is an NullPointerException, eventhough I used findViewById method for recyclerView variable?
I have made this Fragment to Search other followers and users of Instagram clone for Instagram application….But right now unable to find why I am getting this error? AVD is showing it as a fatal error of NullPointerException at the line shown in the code. Please help me out to understand that what is the problem at that line on setHasFixedSize(true) method??

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *