4

I want to pull data from my Firebase database. My table name is "cardornek". And I set the permissions for read/write.

But I get this "java.util.ArrayList.size()' on a null object reference" error. Any idea?

RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

private ArrayList<String> values=null;
RecyclerViewAdapter(ArrayList<String> values) {
    this.values = values;
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item,parent,false));
}


@Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position)
{
    holder.name.setText(values.get(position));
}

@Override
public int getItemCount() {
    return values.size();
}

public class ViewHolder extends RecyclerView.ViewHolder
{
    private TextView name;
    ViewHolder(View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.list_item_text);
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    new GetDataFromFirebase().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    // Read from the database
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("cardornek");

    myRef.addValueEventListener(new ValueEventListener() {


        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ArrayList<String> values = (ArrayList<String>) dataSnapshot.getValue();
            recyclerView.setAdapter(new RecyclerViewAdapter(values));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            System.out.println("Failed to read value." + databaseError.toException());
        }
    });
4
  • dataSnapshot.getValue() returns null so you pass null into the constructur of your RecyclerViewAdapter and when android calls getItemCount() you get a NullPointerException Commented Dec 23, 2016 at 20:44
  • So, what should I do? Commented Dec 23, 2016 at 20:47
  • check the following example: github.com/firebase/quickstart-android/tree/master/database/app/… Commented Dec 23, 2016 at 20:50
  • I'm checking this example github.com/jefrisingh/… cause I don't use Auth process. Commented Dec 23, 2016 at 20:59

1 Answer 1

12

Your problem is that (ArrayList<String>) dataSnapshot.getValue() is sometimes null and after you store values, the exception is thrown in getItemCount. If you are ok with returning 0 as size when values is null, then the solution is

@Override
public int getItemCount() {
    return (values == null) ? 0 : values.size();
}

However, you might want to find out why dataSnapshot.getValue can return null. Maybe that's the real problem and the NullPointerException is just the symptom.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes! Thanks it is working now. But there is no data showing up on cardview. I should check my database I think. Thanks mate!
What would this do?
@Matt it returns 0 of values is null. If not, it returns the size of values. This is a neat way to avoid null pointer exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.