estoy tratando de mostrar una consulta SQlite en un recyclerview pero al tratar de correrla la aplicación se cierra y me indica que el Array esta vacío, alguien me podría dar una mano para saber en que punto podría estar fallando.
Error Log
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference at com.example.formsaide.Adaptadores.AdaptadorRegistros.getItemCount(AdaptadorRegistros.java:85)
Este es el código de la clase Consulta del activity
public class Consulta extends AppCompatActivity {
RecyclerView recyclerViewRegistros; //referencia al recyclerView creado
ArrayList<Items> listaItems; //lista de tipo Items para guardar los datos consultados
ConexionSQLiteHelper conn; //conexion a la base de datos
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_consulta);
conn = new ConexionSQLiteHelper(getApplicationContext(), "bd_datos", null, 1);
//instancia y casteo al recyclerView creado
recyclerViewRegistros = findViewById(R.id.recyclerView);
// se indica al recycler como se cargaran los datos, por defecto se deja this para que lo tome vertical
recyclerViewRegistros.setLayoutManager(new LinearLayoutManager(this));
//metodo encargado de generar la consulta a la base de datos
consultaRegistros();
//instancia del array listaItems
listaItems = new ArrayList<>();
//ListAdapter adaptador = new ArrayAdapter(this, android.R.layout.simple_list_item_1,listaItems);
//referecia a AdaptadorRegistros
AdaptadorRegistros adapter = new AdaptadorRegistros(listaItems);
recyclerViewRegistros.setAdapter(adapter);
}
private void consultaRegistros() {
//se abre la base en modo lectura
SQLiteDatabase db = conn.getReadableDatabase();
//se instancia el objeto Items para llear la información
Items items = null;
// se realiza la referencia Cursor para consulta a la tabla datos
Cursor cursor = db.rawQuery("SELECT * FROM " + Utilidades.TABLA_DATOS, null);
//se creea una lista para la clase Items
listaItems = new ArrayList<Items>();
// se recorre con un While el resultado almacenado en cursor y luego se almacena en la instancia del objeto Items
while (cursor.moveToNext()) {
items = new Items(); //instancia del objeto Items, al final se agrega a la listaItems
items.setId(cursor.getInt(0));
items.setFecha(cursor.getString(1));
items.setcodigo(cursor.getString(2));
items.setObservaciones(cursor.getString(45));
items.setR1_ma(cursor.getString(5));
items.setR1_pc(cursor.getString(6));
items.setR1_tt(cursor.getString(7));
items.setR1_tr(cursor.getString(8));
items.setR1_td(cursor.getString(9));
items.setR1_tc(cursor.getString(10));
items.setR1_ca(cursor.getString(11));
items.setR1_fito(cursor.getString(12));
items.setR1_sc(cursor.getString(13));
items.setR1_cap(cursor.getString(14));
items.setR2_ma(cursor.getString(15));
items.setR2_pc(cursor.getString(16));
items.setR2_tt(cursor.getString(17));
items.setR2_tr(cursor.getString(18));
items.setR2_td(cursor.getString(19));
items.setR2_tc(cursor.getString(20));
items.setR2_ca(cursor.getString(21));
items.setR2_fito(cursor.getString(22));
items.setR2_sc(cursor.getString(23));
items.setR2_cap(cursor.getString(24));
items.setR3_ma(cursor.getString(25));
items.setR3_pc(cursor.getString(26));
items.setR3_tt(cursor.getString(27));
items.setR3_tr(cursor.getString(28));
items.setR3_td(cursor.getString(29));
items.setR3_tc(cursor.getString(30));
items.setR3_ca(cursor.getString(31));
items.setR3_fito(cursor.getString(32));
items.setR3_sc(cursor.getString(33));
items.setR3_cap(cursor.getString(34));
items.setR4_ma(cursor.getString(35));
items.setR4_pc(cursor.getString(36));
items.setR4_tt(cursor.getString(37));
items.setR4_tr(cursor.getString(38));
items.setR4_td(cursor.getString(39));
items.setR4_tc(cursor.getString(40));
items.setR4_ca(cursor.getString(41));
items.setR4_fito(cursor.getString(42));
items.setR4_sc(cursor.getString(43));
items.setR4_cap(cursor.getString(44));
listaItems.add(items);
}
}
Este es el código de mi adaptador para el RecyclerView
public class AdaptadorRegistros extends RecyclerView.Adapter<AdaptadorRegistros.ViewHolderREgistros> {
//ArrayList<String> listaItems;
ArrayList<Items> listaItems;
public AdaptadorRegistros(ArrayList<Items> listaItems) {
}
@NonNull
@Override
public ViewHolderREgistros onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//Se infla el layout auxiliar
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lregistros,null,false);
//Se retorna una instancia nueva de ViewHolderREgistros
return new ViewHolderREgistros(view);
}
@Override
//Comunica a ViewHolderREgistros y el adaptador
public void onBindViewHolder(@NonNull ViewHolderREgistros holder, int position) {
//con holder se crea un método que asigna el Array que trae los datos
holder.fecha.setText((CharSequence) listaItems.get(position).getFecha());
holder.codigo.setText((CharSequence) listaItems.get(position).getcodigo());
holder.ma1.setText((CharSequence) listaItems.get(position).getR1_ma());
holder.pc1.setText((CharSequence) listaItems.get(position).getR1_pc());
holder.tt1.setText((CharSequence) listaItems.get(position).getR1_tt());
holder.tr1.setText((CharSequence) listaItems.get(position).getR1_tr());
holder.td1.setText((CharSequence) listaItems.get(position).getR1_td());
holder.tc1.setText((CharSequence) listaItems.get(position).getR1_tc());
holder.ca1.setText((CharSequence) listaItems.get(position).getR1_ca());
holder.fito1.setText((CharSequence) listaItems.get(position).getR1_fito());
holder.sc1.setText((CharSequence) listaItems.get(position).getR1_sc());
holder.cap1.setText(listaItems.get(position).getR1_cap());
holder.ma2.setText((CharSequence) listaItems.get(position).getR2_ma());
holder.pc2.setText((CharSequence) listaItems.get(position).getR2_pc());
holder.tt2.setText((CharSequence) listaItems.get(position).getR2_tt());
holder.tr2.setText((CharSequence) listaItems.get(position).getR2_tr());
holder.td2.setText((CharSequence) listaItems.get(position).getR2_td());
holder.tc2.setText((CharSequence) listaItems.get(position).getR2_tc());
holder.ca2.setText((CharSequence) listaItems.get(position).getR2_ca());
holder.fito2.setText((CharSequence) listaItems.get(position).getR2_fito());
holder.sc2.setText((CharSequence) listaItems.get(position).getR2_sc());
holder.cap2.setText((CharSequence) listaItems.get(position).getR2_cap());
holder.ma3.setText((CharSequence) listaItems.get(position).getR3_ma());
holder.pc3.setText((CharSequence) listaItems.get(position).getR3_pc());
holder.tt3.setText((CharSequence) listaItems.get(position).getR3_tt());
holder.tr3.setText((CharSequence) listaItems.get(position).getR3_tr());
holder.td3.setText((CharSequence) listaItems.get(position).getR3_td());
holder.tc3.setText((CharSequence) listaItems.get(position).getR3_tc());
holder.ca3.setText((CharSequence) listaItems.get(position).getR3_ca());
holder.fito3.setText((CharSequence) listaItems.get(position).getR3_fito());
holder.sc3.setText((CharSequence) listaItems.get(position).getR3_sc());
holder.cap3.setText((CharSequence) listaItems.get(position).getR3_cap());
holder.ma4.setText((CharSequence) listaItems.get(position).getR4_ma());
holder.pc4.setText((CharSequence) listaItems.get(position).getR4_pc());
holder.tt4.setText((CharSequence) listaItems.get(position).getR4_tt());
holder.tr4.setText((CharSequence) listaItems.get(position).getR4_tr());
holder.td4.setText((CharSequence) listaItems.get(position).getR4_td());
holder.tc4.setText((CharSequence) listaItems.get(position).getR4_tc());
holder.ca4.setText((CharSequence) listaItems.get(position).getR4_ca());
holder.fito4.setText((CharSequence) listaItems.get(position).getR4_fito());
holder.sc4.setText((CharSequence) listaItems.get(position).getR4_sc());
holder.cap4.setText((CharSequence) listaItems.get(position).getR4_cap());
holder.obser.setText((CharSequence) listaItems.get(position).getObservaciones());
}
@Override
public int getItemCount() {
return listaItems.size();
}
public class ViewHolderREgistros extends RecyclerView.ViewHolder {
TextView codigo,fecha,
ma1,pc1,tt1,tr1,td1,tc1,ca1,fito1,sc1,cap1,
ma2,pc2,tt2,tr2,td2,tc2,ca2,fito2,sc2,cap2,
ma3,pc3,tt3,tr3,td3,tc3,ca3,fito3,sc3,cap3,
ma4,pc4,tt4,tr4,td4,tc4,ca4,fito4,sc4,cap4,obser;
public ViewHolderREgistros(@NonNull View itemView) {
super(itemView);
codigo = (TextView) itemView.findViewById(R.id.tvCodigo);
fecha = (TextView) itemView.findViewById(R.id.tvFecha);
ma1 =(TextView) itemView.findViewById(R.id.tvMa1);
pc1 =(TextView) itemView.findViewById(R.id.tvPC1);
tt1 =(TextView) itemView.findViewById(R.id.tvTT1);
tr1 =(TextView) itemView.findViewById(R.id.tvTR1);
td1 =(TextView) itemView.findViewById(R.id.tvTD1);
tc1 =(TextView) itemView.findViewById(R.id.tvTC1);
ca1 =(TextView) itemView.findViewById(R.id.tvCA1);
fito1 =(TextView) itemView.findViewById(R.id.tvFito1);
sc1 =(TextView) itemView.findViewById(R.id.tvSC1);
cap1 =(TextView) itemView.findViewById(R.id.tvCAP1);
ma2 =(TextView) itemView.findViewById(R.id.tvMa2);
pc2 =(TextView) itemView.findViewById(R.id.tvPC2);
tt2 =(TextView) itemView.findViewById(R.id.tvTT2);
tr2 =(TextView) itemView.findViewById(R.id.tvTR2);
td2 =(TextView) itemView.findViewById(R.id.tvTD2);
tc2 =(TextView) itemView.findViewById(R.id.tvTC2);
ca2 =(TextView) itemView.findViewById(R.id.tvCA2);
fito2 =(TextView) itemView.findViewById(R.id.tvFito2);
sc2 =(TextView) itemView.findViewById(R.id.tvSC2);
cap2 =(TextView) itemView.findViewById(R.id.tvCAP2);
ma3 =(TextView) itemView.findViewById(R.id.tvMa3);
pc3 =(TextView) itemView.findViewById(R.id.tvPC3);
tt3 =(TextView) itemView.findViewById(R.id.tvTT3);
tr3 =(TextView) itemView.findViewById(R.id.tvTR3);
td3 =(TextView) itemView.findViewById(R.id.tvTD3);
tc3 =(TextView) itemView.findViewById(R.id.tvTC3);
ca3 =(TextView) itemView.findViewById(R.id.tvCA3);
fito3 =(TextView) itemView.findViewById(R.id.tvFito3);
sc3 =(TextView) itemView.findViewById(R.id.tvSC3);
cap3 =(TextView) itemView.findViewById(R.id.tvCAP3);
ma4 =(TextView) itemView.findViewById(R.id.tvMa4);
pc4 =(TextView) itemView.findViewById(R.id.tvPC4);
tt4 =(TextView) itemView.findViewById(R.id.tvTT4);
tr4 =(TextView) itemView.findViewById(R.id.tvTR4);
td4 =(TextView) itemView.findViewById(R.id.tvTD4);
tc4 =(TextView) itemView.findViewById(R.id.tvTC4);
ca4 =(TextView) itemView.findViewById(R.id.tvCA4);
fito4 =(TextView) itemView.findViewById(R.id.tvFito4);
sc4 =(TextView) itemView.findViewById(R.id.tvSC4);
cap4 =(TextView) itemView.findViewById(R.id.tvCAP4);
obser =(TextView) itemView.findViewById(R.id.tvObser);
}
}