1

I am working on a Firebase Blogging App, using FirebaseRecyclerAdapter. The main problem is occuring due to the setAdapter in onStart(), due to this problem is occurring As a run that activity my Log record popups the following errors:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
    at com.firebase.ui.database.FirebaseArray.onCreate(FirebaseArray.java:54)
    at com.firebase.ui.common.BaseObservableSnapshotArray.addChangeEventListener(BaseObservableSnapshotArray.java:97)
    at com.firebase.ui.database.FirebaseRecyclerAdapter.startListening(FirebaseRecyclerAdapter.java:49)
    at com.example.lenovo.skanda.QuoteList.onStart(QuoteList.java:58)

I am getting error on setAdapter.

QuoteList.java

public class QuoteList extends AppCompatActivity {

    private RecyclerView mQuotesList;

    private DatabaseReference mDatabase;


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

        mQuotesList = (RecyclerView) findViewById(R.id.quotes_list);
        mQuotesList.setHasFixedSize(true);
        mQuotesList.setLayoutManager(new LinearLayoutManager(this));

    }

    @Override
    protected void onStart() {
        super.onStart();

        FirebaseRecyclerOptions<Quote> options =
                new FirebaseRecyclerOptions.Builder<Quote>()
                        .setQuery(mDatabase, Quote.class)
                        .build();

        FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Quote, QuoteViewHolder>(options) {
            @Override
            public QuoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.quote_row, parent, false);
                return new QuoteViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(QuoteViewHolder holder, int position, Quote model) {
                holder.setQuote(model.getQuotes());
                holder.setAuthor(model.getQuote_Author());
            }
        };
        mDatabase.setAdapter(adapter); 
    }

    public static class QuoteViewHolder extends RecyclerView.ViewHolder{
        View mView;
        public QuoteViewHolder(View itemView){
            super(itemView);
            mView = itemView;
        }

        public void setQuote(String Quote){
            TextView post_quote = (TextView) mView.findViewById(R.id.post_quote);
            post_quote.setText(Quote);
        }

        public void setAuthor(String Author){
            TextView post_author = (TextView) mView.findViewById(R.id.post_author);
            post_author.setText(Author);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

}

Quote.java

package com.example.lenovo.skanda;

public class Quote {

    private String Images, Quote_Author, Quotes;

    public Quote(){

    }

    public Quote(String images, String quote_Author, String quotes) {
        Images = images;
        Quote_Author = quote_Author;
        Quotes = quotes;
    }

    public String getImages() {
        return Images;
    }

    public void setImages(String images) {
        Images = images;
    }

    public String getQuote_Author() {
        return Quote_Author;
    }

    public void setQuote_Author(String quote_Author) {
        Quote_Author = quote_Author;
    }

    public String getQuotes() {
        return Quotes;
    }

    public void setQuotes(String quotes) {
        Quotes = quotes;
    }
}

Getting error in code:` mDatabase.setAdapter(adapter);

that cannot resolve method 'setAdapter(com.firebase.ui.database.FirebaseRecyclerAdapter)'`

Please help me out.. Thank you so much in Advance.

itz Prateek
  • 101
  • 1
  • 13
  • mdatabase is not referencing anything.. – Peter Haddad Nov 01 '18 at 20:24
  • yeah.. but how to do so – itz Prateek Nov 01 '18 at 20:39
  • @PeterHaddad i took the reference from the following link:https://stackoverflow.com/questions/47091837/firebaserecycleradapter-cannot-be-applied-to-firebaserecycleradapter – itz Prateek Nov 01 '18 at 20:41
  • https://stackoverflow.com/questions/47364513/why-i-cant-use-the-populateviewholder-override-method – Peter Haddad Nov 01 '18 at 20:43
  • @PeterHaddad How to attach the adapter to your RecyclerView with the RecyclerView#setAdapter() method. – itz Prateek Nov 01 '18 at 20:50
  • @PeterHaddad please help me out.. – itz Prateek Nov 02 '18 at 05:37
  • I gave u the link, you just need to read the answer. You are getting null point exception which means that mDatabase is not referencing anything in the heap(it's not pointing to anything or in other words its equal to null...) all you need to do is `DatabaseReference mDatabase=FirebaseDatabase.getInstance().getReference().child("your_child_name");` – Peter Haddad Nov 02 '18 at 17:01

0 Answers0