Ich versuche ArrayList
, die aus Person-Objekten besteht, abzurufen. Hier ist der Teil, der den Fehler in dem Dokument (2. Reihe) ergibt:java.lang.NullPointerException: Versuch, die virtuelle Methode aufzurufen 'java.util.ArrayList com.example. ***. ***. DBHelper.getAllPersons()
ArrayList<Person> persons = new ArrayList<Person>();
persons = myDb.getAllPersons();
Hier ist die Person Klasse:
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;
}
}
Hier ist die Methode der DBHelper
Klasse, die die ArrayList
von Person-Objekte abruft:
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;
}
Was könnte das verursachen? Ich habe keine Ahnung, seit die ArrayList
Personen nicht null ist, seit es initialisiert ist.
Hast du das 'myDb'-Objekt initialisiert? –