![](/img/trans.png)
[英]Getting null pointer exception while inserting into two dimensional array of Strings
[英]Android: getting Null Pointer Exception when inserting in Array
我收到錯誤“java.lang.NullPointerException:嘗試寫入空數組”,當從數據庫獲取一些數據后,我必須將該數據放入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);
你能告訴我怎樣才能解決這個錯誤?
Gasto[] items=null;
你必須在訪問之前創建Gastos
數組:
Gasto[] items = new Gastos[gastos.size()];
Gasto[] items = null;
是問題。 插入時不會創建數組。 使用Gasto[] items = new Gasto[gastos.size()];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.