从Sqlite获取ArrayList时获取java.lang.NullPointerException

时间:2015-12-06 14:23:31

标签: java android sqlite arraylist alertdialog

按下按钮时,我收到错误

java.lang.NullPointerException
at com.gor.hocam.MainActivity.getListitem(MainActivity.java:329)
at com.gor.hocam.MainActivity$2.onClick(MainActivity.java:222)
at android.view.View.performClick(View.java:4445)
at android.view.View$PerformClick.run(View.java:18446)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5139)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at dalvik.system.NativeStart.main(Native Method)

我点击的按钮是(MainActivity.java:222是下面的第一行);

getListitem();
System.out.println(dersadilist.size());

String[] arrRecords = dersadilist.toArray(new String[dersadilist.size()]);

int arr[] = new int[derskodulist.size()];
for (int i = 0; i < derskodulist.size(); i++) {
    arr[i] = Integer.parseInt(derskodulist.get(i));
}



new MaterialDialog.Builder(MainActivity.this)
    .title("Son Çözdüğünüz Dersler:")
    .items(arrRecords)
    .itemsIds(arr)
    .itemsCallback(new MaterialDialog.ListCallback() {
        @Override
        public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
            Toast.makeText(MainActivity.this, which + ": " + text + ", ID = " + view.getId(), Toast.LENGTH_SHORT).show();
        }
    }).show();

这是我的getListitem(=(MainActivity.java:329);

    public void getListitem() {

    List<Scoredata> score = db.getAllContacts();

    for (Scoredata sc : score) {
        dersidler = String.valueOf(sc.getId());
        dersadlari = sc.getName();
        topscore = sc.getScore();
        derskodlari = sc.getKodu();
        //sc.getScore();

        dersidlist.add(dersidler);
        dersadilist.add(dersadlari);
        scorelist.add(topscore);
        derskodulist.add(dersadlari);
    }



}

这就是我的Scoredata;

public class Scoredata {

int id;
String name, kodu, score;
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 String getKodu() {
    return kodu;
}
public void setKodu(String kodu) {
    this.kodu = kodu;
}
public String getScore() {
    return score;
}
public void setScore(String score) {
    this.score = score;
}

public Scoredata(String name, String kodu, String score) {
    super();

    this.name = name;
    this.kodu = kodu;
    this.score = score;
}
public Scoredata() {
    super();
    // TODO Auto-generated constructor stub
}

}

getAllContacts在我的DbHighestScore上;

public List<Scoredata> getAllContacts() {
    List<Scoredata> scoreList = new ArrayList<Scoredata>();



    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS + " ORDER BY id DESC";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Scoredata score = new Scoredata();
            score.setId(Integer.valueOf(cursor.getString(0)));
            score.setName(cursor.getString(1));
            score.setKodu(cursor.getString(2));
            score.setScore(cursor.getString(3));


            scoreList.add(score);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();

    // return contact list
    return scoreList;
}

1 个答案:

答案 0 :(得分:0)

在解析之前检查db对象是否为空且集合对象score不为空

public void getListitem() {
    if(db != null){
    List<Scoredata> score = db.getAllContacts();
    if(score!= null){
    for (Scoredata sc : score) {
        dersidler = String.valueOf(sc.getId());
        dersadlari = sc.getName();
        topscore = sc.getScore();
        derskodlari = sc.getKodu();
        //sc.getScore();

        dersidlist.add(dersidler);
        dersadilist.add(dersadlari);
        scorelist.add(topscore);
        derskodulist.add(dersadlari);
    }
}
}
}