-3

i know this question has been asked before but my code is still not working after applying the patches. when i touch the fetch button it crashes and shows the error

java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.recyclerview.widget.RecyclerView$Adapter androidx.recyclerview.widget.RecyclerView.getAdapter()' on a null object reference
    at com.example.csitcms.myrecyclerviewActivity$1.onChildAdded(myrecyclerviewActivity.java:35)'

The last line of the code shows the error below:

public class myrecyclerviewActivity extends AppCompatActivity {
    RecyclerView recyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_recycler_view);

        DatabaseReference databaseReference= FirebaseDatabase.getInstance().getReference();
        databaseReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                String filename= dataSnapshot.getKey();
                String url= dataSnapshot.getValue(String.class);

                ((MyAdapter)recyclerView.getAdapter()).update(filename,url);
            }
dangee1705
  • 3,445
  • 1
  • 21
  • 40
  • sir please help me with the last line, what can i add to the code so that the fetch button doesn't crashes?? – Alice xane Aug 22 '19 at 16:50
  • Where do you instantiate `recyclerView`? i.e. `recyclerView = /* something goes here */;` – Jonny Henly Aug 22 '19 at 16:51
  • In MyAdapter class, the code is below: { RecyclerView recyclerview; Context context; ArrayList items= new ArrayList<>(); ArrayList urls= new ArrayList<>(); – Alice xane Aug 22 '19 at 16:52
  • The Array lists are also blurred with the message "the code is redundant or never being used after this." – Alice xane Aug 22 '19 at 16:54
  • A Class Instance or Variable by default for an object (non-intiazlied) is always null, in your case recyclerView is null, after setContentView you need to assign the value to this recyclerView and map with your id that you defined in the XML layout my_recycler_view. – Yauraw Gadav Aug 22 '19 at 17:53

1 Answers1

0

RecylerView is never initialized. You need to recyclerView = findViewById(R.id.theIdOfYourRecyclerView) somewhere in onCreate() (but after setContentView).

Also, replace theIdOfYourRecyclerView with the id of your RecyclerView.

Axiumin_
  • 2,107
  • 2
  • 15
  • 24
  • I initialized the recyclerView in the oncreate method but still the app is crashing. The debugger shows line 35: ((MyAdapter)recyclerView.getAdapter()).update(filename,url); The update has been shown in yellow. – Alice xane Aug 22 '19 at 17:00
  • how did you initialize it? @Alicexane – Axiumin_ Aug 22 '19 at 17:00
  • Like this: public class myrecyclerviewActivity extends AppCompatActivity { RecyclerView recyclerView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_recycler_view); recyclerView=findViewById(R.id.recyclerview); – Alice xane Aug 22 '19 at 17:02
  • what is the error @Alicexane – Axiumin_ Aug 22 '19 at 17:10