I'm getting the error "java.lang.NullPointerException: Attempt to write to null array" when, after getting some data from database I have to put that data in a ArrayAdapter.
DatabaseHelper databaseHelper = new DatabaseHelper(this.getApplicationContext());
List <Gasto> gastos;
gastos = databaseHelper.getAllGastos();
Gasto[] items=null;
for(int i=0;i<gastos.size();i++)
{
items[i] = new Gasto(gastos.get(i).getMes(),gastos.get(i).getAno(), gastos.get(i).getDespesa_final());
}
dataAdapter = new ArrayAdapter<Gasto>(this,android.R.layout.simple_list_item_1, items);
Can you please tell me how can I solve this error?
Gasto[] items = null;
is the problem. Array is not created when you insert to it. UseGasto[] items = new Gasto[gastos.size()];
you have to create the array of
Gastos
before accessing it: