当我运行应用程序时,强制关闭。此应用程序功能是使用asynctask从listview中的sqlite显示数据。但我在
的nullpointer中得到错误collegeList.add(地图); - >空指针
然后,我想要的是,数据将按id显示。 id是外键。
这是我的代码:
private class GetHttpResponse extends AsyncTask<Void, Void, Void> {
ArrayList<HashMap<String, String>> collegeList;
protected void onPreExecute()
{ super.onPreExecute();}
protected Void doInBackground(Void... arg0) {
List<Rainday> raindaydetail = new LinkedList<Rainday>();
raindaydetail=dbHelper.getAllRainday();
for(Rainday temp_vg: raindaydetail){
temp_vg.getId();
temp_vg.getYear_rainday();
temp_vg.getJan();
temp_vg.getFeb();
temp_vg.getMar();
temp_vg.getApr();
temp_vg.getMay();
temp_vg.getJun();
temp_vg.getJul();
temp_vg.getAug();
temp_vg.getSep();
temp_vg.getOct();
temp_vg.getNov();
temp_vg.getDec();
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("id", String.valueOf(temp_vg.getId()));
map.put("year_rainfall", temp_vg.getYear_rainday());
map.put("jan", temp_vg.getJan());
map.put("feb", temp_vg.getFeb());
map.put("mar", temp_vg.getMar());
map.put("apr", temp_vg.getApr());
map.put("may", temp_vg.getMay());
map.put("jun", temp_vg.getJun());
map.put("jul", temp_vg.getJul());
map.put("aug", temp_vg.getAug());
map.put("sep", temp_vg.getSep());
map.put("oct", temp_vg.getOct());
map.put("nov", temp_vg.getNov());
map.put("dec", temp_vg.getDec());
collegeList.add(map);
}
return null;
}
protected void onPostExecute(Void result)
{
proCollageList.setVisibility(View.GONE);
listCollege.setVisibility(View.VISIBLE);
if (collegeList != null) {
ListAdapterRainday adapter = new ListAdapterRainday(RaindayReport.this, collegeList);
listCollege.setAdapter(adapter);}}
MySQLiteHelper:
public List<Rainday> getAllRainday() {
List<Rainday> rainday = new LinkedList<Rainday>();
// 1. build the query
String query = "SELECT * FROM " + TABLE_RAINDAY + " WHERE id = " + KEY_ID_RAINDAY;
// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// 3. go over each row, build book and add it to list
Rainday rainday1 = null;
if (cursor.moveToFirst()) {
do {
rainday1 = new Rainday();
rainday1.setId(Integer.parseInt(cursor.getString(0)));
rainday1.setYear_rainday(cursor.getString(1));
rainday1.setJan(cursor.getString(2));
rainday1.setFeb(cursor.getString(3));
rainday1.setMar(cursor.getString(4));
rainday1.setApr(cursor.getString(5));
rainday1.setMay(cursor.getString(6));
rainday1.setJun(cursor.getString(7));
rainday1.setJul(cursor.getString(8));
rainday1.setAug(cursor.getString(9));
rainday1.setSep(cursor.getString(10));
rainday1.setOct(cursor.getString(11));
rainday1.setNov(cursor.getString(12));
rainday1.setDec(cursor.getString(13));
// Add vgm
rainday.add(rainday1);
} while (cursor.moveToNext());
}
return rainday;
}
我需要你的帮助。如果有人帮助我,我会很感激。
答案 0 :(得分:2)
我想您忘了初始化 collegeList
只是像这样初始化它
ArrayList<HashMap<String, String>> collegeList;
collegeList= new ArrayList<>();
答案 1 :(得分:0)
在我的代码中,我找到了以下行
ArrayList<HashMap<String, String>> collegeList;
并且您没有在代码中的任何位置初始化collegeList