-2
私は、Personオブジェクトで構成されるArrayList
を取得しようとしています。ここでタイトル(2行目)に誤差を与える部分は次のとおりjava.lang.NullPointerException:仮想メソッド 'java.util.ArrayListを呼び出そうとしました。com.example。***。***。DBHelper.getAllPersons()
ArrayList<Person> persons = new ArrayList<Person>();
persons = myDb.getAllPersons();
ここでPersonクラスは次のとおり
:public class Person {
private int id;
private String name;
private int age;
private double locationX;
private double locationY;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getLocationX() {
return locationX;
}
public void setLocationX(double locationX) {
this.locationX = locationX;
}
public double getLocationY() {
return locationY;
}
public void setLocationY(double locationY) {
this.locationY = locationY;
}
}
ここでPersonオブジェクトのArrayList
を取得DBHelper
クラスのメソッドです
public ArrayList<Person> getAllPersons() {
ArrayList<Person> array_list = new ArrayList<Person>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from " + ITEMS_TABLE_NAME + " order by item asc", null);
res.moveToFirst();
while(res.isAfterLast() == false){
Person person = new Person();
person.setName(res.getString(res.getColumnIndex(ITEMS_COLUMN_NAME)));
person.setAge(res.getInt(res.getColumnIndex(ITEMS_COLUMN_AGE)));
person.setLocationX(res.getDouble(res.getColumnIndex(ITEMS_COLUMN_LOCATIONX)));
person.setLocationY(res.getDouble(res.getColumnIndex(ITEMS_COLUMN_LOCATIONY)));
array_list.add(person);
res.moveToNext();
}
return array_list;
}
これはどうしてですか? ArrayList
人は初期化されてからヌルではないので、私は分かりません。
'myDb'オブジェクトを初期化しましたか? –