从数据库填充列表时的NPE

时间:2013-01-02 16:28:05

标签: android database listview nullpointerexception

Class#1 - 必须从DB获取数据并准备ListAdapter:

public class DataListView extends ListActivity {

    public ArrayList<String> results = new ArrayList<String>();
    public String tableName = DBHelper.tableName;
    public SQLiteDatabase newDB;

    public void displayResultList() {
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results));
        getListView().setTextFilterEnabled(true);
    }

    public void openAndQueryDatabase() {
        try {
            DBHelper dbHelper = new DBHelper(this.getApplicationContext());
            newDB = dbHelper.getWritableDatabase();
            Cursor c = newDB.rawQuery("SELECT FirstName, Age FROM " +
                    tableName, null);

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String firstName = c.getString(c.getColumnIndex("FirstName"));
                        int age = c.getInt(c.getColumnIndex("Age"));
                        results.add("Name: " + firstName + ", Age: " + age);
                    }while (c.moveToNext());
                } 
            }           
        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        } finally {
            if (newDB != null) 
                newDB.execSQL("DELETE FROM " + tableName);
                newDB.close();
        }

    }
}

Class#2 - 必须显示列表:

public class StartActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         DataListView list = new DataListView();
         list.openAndQueryDatabase();
         list.displayResultList();
    }
}

但它引发了一个错误:

  

01-02 18:22:25.584:E / AndroidRuntime(30425):致命异常:主要   01-02 18:22:25.584:E / AndroidRuntime(30425):   java.lang.RuntimeException:无法启动活动   ComponentInfo {com.saigmn / com.saigmn.StartActivity}:   java.lang.NullPointerException 01-02 18:22:25.584:   E / AndroidRuntime(30425):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)   01-02 18:22:25.584:E / AndroidRuntime(30425):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)   01-02 18:22:25.584:E / AndroidRuntime(30425):at   android.app.ActivityThread.access $ 600(ActivityThread.java:123)01-02   18:22:25.584:E / AndroidRuntime(30425):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)   01-02 18:22:25.584:E / AndroidRuntime(30425):at   android.os.Handler.dispatchMessage(Handler.java:99)01-02   18:22:25.584:E / AndroidRuntime(30425):at   android.os.Looper.loop(Looper.java:137)01-02 18:22:25.584:   E / AndroidRuntime(30425):at   android.app.ActivityThread.main(ActivityThread.java:4424)01-02   18:22:25.584:E / AndroidRuntime(30425):at   java.lang.reflect.Method.invokeNative(Native Method)01-02   18:22:25.584:E / AndroidRuntime(30425):at   java.lang.reflect.Method.invoke(Method.java:511)01-02 18:22:25.584:   E / AndroidRuntime(30425):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)   01-02 18:22:25.584:E / AndroidRuntime(30425):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)01-02   18:22:25.584:E / AndroidRuntime(30425):at   dalvik.system.NativeStart.main(Native Method)01-02 18:22:25.584:   E / AndroidRuntime(30425):引起:java.lang.NullPointerException   01-02 18:22:25.584:E / AndroidRuntime(30425):at   com.saigmn.DataListView.openAndQueryDatabase(DataListView.java:45)   01-02 18:22:25.584:E / AndroidRuntime(30425):at   com.saigmn.StartActivity.onCreate(StartActivity.java:20)01-02   18:22:25.584:E / AndroidRuntime(30425):at   android.app.Activity.performCreate(Activity.java:4492)01-02   18:22:25.584:E / AndroidRuntime(30425):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)   01-02 18:22:25.584:E / AndroidRuntime(30425):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

经过一些测试后,我发现openAndQueryDatabase()中有smth。

当将两个被调用的方法放入StartActivity类时 - 它工作正常。

请帮我找出这个问题。

1 个答案:

答案 0 :(得分:2)

if (newDB != null) 
  newDB.execSQL("DELETE FROM " + tableName);
  newDB.close();

这可能可能是来源。将两个语句放在if块中,以确保close() null实例上没有进行newDB调用。尝试:

if (newDB != null) 
{ 
  newDB.execSQL("DELETE FROM " + tableName);
  newDB.close();
}

现在我很确定这是你的NullPointer的来源。检查DataListView.java中的第45行。