从sqlite表中获取String到数组

时间:2015-11-12 09:16:34

标签: java android sqlite android-sqlite

我尝试从sqlite

中的表中获取数据

DatabaseHelper.java

...
public Cursor getAllData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
    return res;
}
...

MainActivity.java

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Cursor ress = myDb.getPartData();
    String[] id_pull = new String[ress.getCount()];

    if (ress.getCount() == 0) {
        int i = 0;
        while (ress.moveToNext()) {
            String uname = ress.getString(ress.getColumnIndex("ID"));
            id_pull[i] = uname;
            i++;
        }
    }else
    {Toast.makeText(MainActivity.this, "ID not found", Toast.LENGTH_LONG).show();}


...

我的sqlite表上有4条记录,我尝试获取字符串数据并推送到数组(id_pull)但是当我运行这个应用程序时,应用程序强制关闭。 任何人都可以帮助修复我的代码:)

感谢

记录详细

  

11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:FATAL EXCEPTION:main   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:进程:com.app.example.sqliteapp,PID:22191   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:java.lang.RuntimeException:无法启动活动ComponentInfo {com.app.example.sqliteapp / com.app.example.sqliteapp .MainActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.database.Cursor com.app.example.sqliteapp.DatabaseHelper.getPartData()'   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.app.ActivityThread.access $ 800(ActivityThread.java:148)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1292)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:102)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.os.Looper.loop(Looper.java:135)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.app.ActivityThread.main(ActivityThread.java:5312)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at java.lang.reflect.Method.invoke(Native Method)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at java.lang.reflect.Method.invoke(Method.java:372)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:901)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:引起:java.lang.NullPointerException:尝试调用虚方法'android.database.Cursor com.app.example.sqliteapp .DatabaseHelper.getPartData()'在空对象引用上   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at com.app.example.sqliteapp.MainActivity.onCreate(MainActivity.java:46)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.app.Activity.performCreate(Activity.java:5953)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.app.ActivityThread.access $ 800(ActivityThread.java:148)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1292)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:102)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.os.Looper.loop(Looper.java:135)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at android.app.ActivityThread.main(ActivityThread.java:5312)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at java.lang.reflect.Method.invoke(Native Method)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at java.lang.reflect.Method.invoke(Method.java:372)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:901)   11-12 15:51:43.981 22191-22191 / com.app.example.sqliteapp E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

1 个答案:

答案 0 :(得分:1)

在使用之前创建DatabaseHelper:

myDb = new DatabaseHelper();