0

My recycler view is throwing java.lang.NullPointerException:

Attempt to invoke virtual method 'java.util.ArrayList com.msit.example.bunty.regform.entities.DataStore.getDataStore()' on a null object reference at com.msit.example.bunty.regform.adapters.MyRecyclerAdapter.getItemCount(MyRecyclerAdapter.java:65)

MyRecyclerAdapter.java

package com.msit.example.bunty.regform.adapters; import android.content.Context; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.R.*; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.Toast; import com.msit.example.bunty.regform.R; import com.msit.example.bunty.regform.entities.DataStore; import com.msit.example.bunty.regform.entities.UserData; import org.w3c.dom.Text; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; /**l * Created by bunty on 1/5/16. */ public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> { DataStore userDataStore=new DataStore(); //ArrayList<UserData> userDataStore=new ArrayList<UserData>(); public MyRecyclerAdapter(DataStore userDataStore) { this.userDataStore=userDataStore; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false); ViewHolder viewHolder=new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.fNameView.setText(userDataStore.getDataStore().get(position).getFirstName()); holder.lNameView.setText(userDataStore.getDataStore().get(position).getLastName()); holder.addressView.setText(userDataStore.getDataStore().get(position).getAddress()); holder.cityView.setText(userDataStore.getDataStore().get(position).getCity()); final String city=userDataStore.getDataStore().get(position).city; holder.cityView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(v.getContext(),city,Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { if(!userDataStore.getDataStore().isEmpty()&&userDataStore.getDataStore()!=null) { return userDataStore.getDataStore().size(); } else { return 0; } } public static class ViewHolder extends RecyclerView.ViewHolder { TextView fNameView; TextView lNameView; TextView addressView; Button cityView; CardView cardView; public ViewHolder(View itemLayoutView) { super(itemLayoutView); fNameView = (TextView) itemView.findViewById(R.id.fn_view); lNameView = (TextView) itemView.findViewById(R.id.ln_view); addressView = (TextView) itemView.findViewById(R.id.addr_view); cityView=(Button)itemView.findViewById(R.id.city_view); cardView = (CardView) itemLayoutView.findViewById(R.id.card_list); } } } 

I have two buttons Save and show_details,The app crashes when i click show_details if there are no values saved initially and raises an exception in getitemcount() method.

1
  • Before trying to see if it's empty or getDataStore, you should see if userDataStore in not null Commented Jan 8, 2016 at 20:06

3 Answers 3

1

Fix getItemCount method like this

@Override public int getItemCount() { if(userDataStore == null){return 0;} if(!userDataStore.getDataStore().isEmpty()&&userDataStore.getDataStore()!=null) { return userDataStore.getDataStore().size(); } else{ return 0; } } 
Sign up to request clarification or add additional context in comments.

Comments

1

You are initializing the userDataStore in your class, but you assign a new value in the constructor. In this way this variable can be null.

DataStore userDataStore=new DataStore(); public MyRecyclerAdapter(DataStore userDataStore) { this.userDataStore=userDataStore; } 

You should check in your code if it is not null before using it.

In your getItemCount you can have a NPE, due to this reason.

 @Override public int getItemCount() { if(userDataStore != null && !userDataStore.getDataStore().isEmpty() && userDataStore.getDataStore()!=null) { return userDataStore.getDataStore().size(); } else{ return 0; } } 

Comments

0

You initialize the Datastore in the Adapter and then in the constructor the param is a DataStore which means that you are already passing a newly created DataStore in the Activity that you are calling your Adapter. So just simplify your code:

DataStore userDataStore; public MyRecyclerAdapter(DataStore userDataStore){ this.userDataStore=userDataStore; } @Override public int getItemCount() { return userDataStore.getDataStore().size(); } 

Hope it helps!!!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.