Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I wanted to search data from database to recycler view, my code is below but I get an error in line 20 please help me to resolve the issue.

Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
        at com.nyt.kitaab.lughatulquran.MainActivity.onCreate(MainActivity.java:85)



This line has the error:
recyclerView.setLayoutManager(new LinearLayoutManager(this));


Thanks in advance.

Sorry I forgot this line:
recyclerView = findViewById(R.id.recycler);


What I have tried:

Java
  1  public class MainActivity extends AppCompatActivity {
  2      EditText searchtext;
  3      RecyclerView recyclerView;
  4      RecyclerView.LayoutManager layoutManager;
  5      MyAdapter adapter;
  6      Database db;
  7      AdView mAdView;
  8      Context context;
  9  
 10      @Override
 11      protected void onCreate(Bundle savedInstanceState) {
 12          super.onCreate(savedInstanceState);
 13          setContentView(R.layout.activity_main);
 14          mAdView = findViewById(R.id.adView);
 15          AdRequest adRequest = new AdRequest.Builder().build();
 16          mAdView.loadAd(adRequest);
 17          db = new Database(this);
 18          List<LughatDetail> lughatList = new ArrayList<>();
 19          searchtext = findViewById(R.id.searchText);
 20          recyclerView.setLayoutManager(new LinearLayoutManager(this));
 21          recyclerView.setHasFixedSize(true);
 22  
 23          searchtext.addTextChangedListener(new TextWatcher() {
 24              @Override
 25              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 26  
 27              }
 28  
 29              @Override
 30              public void onTextChanged(CharSequence s, int start, int before, int count) {
 31  
 32              }
 33  
 34              @Override
 35              public void afterTextChanged(Editable s) {
 36                  lughatList.clear();
 37                  if(s.toString().isEmpty()){
 38                      // Do something
 39                  }
 40                  else {
 41                      searchLughat(s.toString());
 42                  }
 43              }
 44  
 45              private void searchLughat(String text) {
 46                  for(LughatDetail lugat:lughatList){
 47                      if(lugat.getField2().equals(text)){
 48                          lughatList.add(lugat);
 49                      }
 50                  }
 51                  recyclerView.setAdapter(new MyAdapter(getApplicationContext(),lughatList));
 52                  adapter.notifyDataSetChanged();
 53              }
 54          });
 55  
 56  
 57  
 58      }
 59  
 60      public class SpeedyLinearLayoutManager extends LinearLayoutManager {
 61  
 62          private static final float MILLISECONDS_PER_INCH = 5f; //default is 25f (bigger = slower)
 63  
 64          public SpeedyLinearLayoutManager(Context context) {
 65              super(context);
 66          }
 67  
 68          public SpeedyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
 69              super(context, orientation, reverseLayout);
 70          }
 71  
 72          public SpeedyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
 73              super(context, attrs, defStyleAttr, defStyleRes);
 74          }
 75  
 76          @Override
 77          public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
 78  
 79              final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
 80  
 81                  @Override
 82                  public PointF computeScrollVectorForPosition(int targetPosition) {
 83                      return super.computeScrollVectorForPosition(targetPosition);
 84                  }
 85  
 86                  @Override
 87                  protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
 88                      return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
 89                  }
 90              };
 91  
 92              linearSmoothScroller.setTargetPosition(position);
 93              startSmoothScroll(linearSmoothScroller);
 94          }
 95      }
 96  }
Posted
Updated 25-Feb-22 4:21am
v7
Comments
Richard MacCutchan 25-Feb-22 3:45am
   
Line 85 is blank, so the code you have posted is not the code you are running.

1 solution

You have declared RecyclerView recyclerView; in your class, but you have not initialised it before trying to use it. So it is still null, and causes the error you see.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900