Guest User

SearchActivity

a guest
Aug 29th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. // I have created this SearchActivity based on a RecyclerView but when I click on an Item on the list I get a NPE:
  2.  
  3. /* java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference */
  4.  
  5. the error is caused by this part of code:
  6.  
  7. ItemClickSupport.addTo(rv).setOnItemClickListener(new ItemClickSupport.OnItemClickListener(){
  8.             @Override
  9.             public void onItemClicked(RecyclerView recyclerView, int position, View v) {
  10.                 Intent intent = new Intent(SearchActivity.this, ChordActivity.class);
  11.                 Bundle bundle = new Bundle();
  12.                 bundle.putParcelable("selected", filteredModelList.get(position)); //HERE IS THE ERROR
  13.                 intent.putExtras(bundle);
  14.                 startActivity(intent);
  15.             }
  16.         });
  17.  
  18. The filter it is based on is this one:
  19.  
  20. private List<Accordo> filter(List<Accordo> models, String query) {
  21.         query = query.toLowerCase();
  22.  
  23.         filteredModelList = new ArrayList<>();
  24.         for (Accordo model : models) {
  25.             final String text = model.getName().toLowerCase();
  26.             if (text.contains(query)) {
  27.                 filteredModelList.add(model);
  28.             }
  29.         }
  30.         return filteredModelList;
  31.     }
  32.  
  33. IMPORTANT: the error accours ONLY when I got the whole list, when it is NOT FILTERED. If I type anything and filter the list and then click on the Item the error does not accour.
Advertisement
Add Comment
Please, Sign In to add comment