Android:复制资产文件夹中的数据库空指针异常

时间:2015-02-21 20:37:05

标签: android database sqlite nullpointerexception

我已经在这里阅读了与我的问题相关的几乎所有主题,所以请不要说它是重复的或者之前已被质疑过。

我在Sqlite Expert Personal 3中创建了表和数据库。我将它复制到assets文件夹中。现在我想告诉android检查是否已经有数据库,从assets文件夹中复制数据库。所以我这样做:

datasource = new DataSource(this);
    List<Prayer> prayers = datasource.findAllPrayers();

    if (prayers.size == 0)
    {
        dbopenhelper.copyDataBase();
    }
    datasource.open();

这里应该检查表格中是否有任何行&#34;祈祷&#34;如果没有任何行,它应该复制数据库。但是当我运行程序时,我得到nullPointerException。


这是我的OpenHelper课程:

package org.bihe.database;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBOpenHelper extends SQLiteOpenHelper
{

    public static final String LOGTAG = "PRAYERBOOK_APP";
    //
    private static Context context;
    //
    private static String DB_PATH;
    //
    public static final String DATABASE_NAME = "Prayers.db";
    public static final int DATABASE_VERSION = 1;
    // ***Tables***
    public static final String TABLE_PRAYERTOPIC = "prayertopic";
    public static final String TABLE_PRAYER = "prayer";
    public static final String TABLE_FAVORITES = "favorites";
    public static final String TABLE_PRAYEROFTOPIC = "prayeroftopic";
    public static final String TABLE_FAVORITEPRAYERS = "favoriteprayers";
    public static final String TABLE_AUDIOPRAYERS = "audioprayers";
    // ***Table prayertopic columns***
    public static final String COLUMN_PRAYERTOPIC_ID = "prayertopic_ID";
    public static final String COLUMN_PRAYERTOPIC_NAME = "prayertopic_name";
    // ***Table prayer columns***
    public static final String COLUMN_PRAYER_ID = "prayer_ID";
    public static final String COLUMN_PRAYER_NAME = "prayer_name";
    public static final String COLUMN_PRAYER = "prayer";
    // ***Table favorites columns***
    public static final String COLUMN_FAVORITE_ID = "favorite_ID";
    public static final String COLUMN_FAVORITE_NAME = "favorite_name";
    // ***Table prayeroftopic columns***
    public static final String COLUMN_PRAYEROFTOPIC_ID = "prayeroftopic_ID";
    public static final String COLUMN_PRAYER_ID_POT = "prayer_ID";
    public static final String COLUMN_PRAYERTOPIC_ID_POT = "prayertopic_ID";
    // ***Table favoriteprayer columns***
    public static final String COLUMN_FAVORITEPRAYER_ID = "favoriteprayer_ID";
    public static final String COLUMN_FAVORITE_ID_FP = "favorite_ID";
    public static final String COLUMN_PRAYER_ID_FP = "prayer_ID";
    // ***Table auidioprayers***
    public static final String COLUMN_AUDIOPRAYER_ID = "audioprayer_ID";
    public static final String COLUMN_AUDIOPRAYER_NAME = "audioprayer_name";
    public static final String COLUMN_AUDIOPRAYER_ADDRESS = "audioprayer_address";

    // ***Creating Tables***
    private static final String CREATE_PRAYER_TABLE = "CREATE TABLE "
            + TABLE_PRAYER + " (" + COLUMN_PRAYER_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_PRAYER_NAME
            + " TEXT, " + COLUMN_PRAYER + " TEXT " + ")";
    //
    private static final String CREATE_PRAYERTOPIC_TABLE = "CREATE TABLE "
            + TABLE_PRAYERTOPIC + " (" + COLUMN_PRAYERTOPIC_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_PRAYERTOPIC_NAME
            + " TEXT" + ")";
    //
    private static final String CREATE_PRAYEROFTOPIC_TABLE = "CREATE TABLE "
            + TABLE_PRAYEROFTOPIC + " (" + COLUMN_PRAYEROFTOPIC_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + " FOREIGN KEY("
            + COLUMN_PRAYER_ID_POT + ") REFERENCES " + TABLE_PRAYER + "("
            + COLUMN_PRAYER_ID + ") ON UPDATE CASCADE ON DELETE CASCADE "
            + " FOREIGN KEY(" + COLUMN_PRAYERTOPIC_ID_POT + ") REFERENCES "
            + TABLE_PRAYERTOPIC + "(" + COLUMN_PRAYERTOPIC_ID
            + ") ON UPDATE CASCADE ON DELETE CASCADE " + ") ";

    public DBOpenHelper(Context ctx)
    {
        super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
        context = ctx;
        DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
    }

    //
    public static void copyDataBase()
    {
        try
        {
            InputStream input = context.getAssets().open(DATABASE_NAME);
            FileOutputStream output = new FileOutputStream(DB_PATH
                    + DATABASE_NAME);
            byte[] data = new byte[1024];
            int buffer;
            while ((buffer = input.read(data)) != -1)
            {
                output.write(data, 0, buffer);
            }
            output.flush();
            output.close();
            input.close();
        } catch (IOException ex)
        {
            Log.e(LOGTAG, "Error in copying database...");
        }
    }

    //

    // ***--***
    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(CREATE_PRAYER_TABLE);
        Log.i(LOGTAG, "Table has been created");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        // db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRAYER);
        onCreate(db);

    }

}

如果有人能告诉我我的代码有什么问题,我将非常感激。感谢提前


编辑:

    02-21 20:06:17.346: E/Trace(4814): error opening trace file: No such file or directory (2)
02-21 20:06:17.896: I/PRAYERBOOK_APP(4814): Database opened
02-21 20:06:17.968: I/PRAYERBOOK_APP(4814): Database opened
02-21 20:06:18.278: D/libEGL(4814): loaded /system/lib/egl/libEGL_emulation.so
02-21 20:06:18.430: D/(4814): HostConnection::get() New Host Connection established 0x2a13b0b8, tid 4814
02-21 20:06:19.047: D/libEGL(4814): loaded /system/lib/egl/libGLESv1_CM_emulation.so
02-21 20:06:19.057: D/libEGL(4814): loaded /system/lib/egl/libGLESv2_emulation.so
02-21 20:06:20.127: W/EGL_emulation(4814): eglSurfaceAttrib not implemented
02-21 20:06:20.169: D/OpenGLRenderer(4814): Enabling debug mode 0
02-21 20:06:20.507: D/dalvikvm(4814): GC_CONCURRENT freed 84K, 7% free 2706K/2908K, paused 91ms+6ms, total 199ms
02-21 20:06:56.497: I/PRAYERBOOK_APP(4814): Database closed
02-21 20:06:57.127: D/dalvikvm(4814): GC_FOR_ALLOC freed 65K, 6% free 2992K/3180K, paused 35ms, total 52ms
02-21 20:06:57.137: I/dalvikvm-heap(4814): Grow heap (frag case) to 4.110MB for 1127536-byte allocation
02-21 20:06:57.217: D/dalvikvm(4814): GC_FOR_ALLOC freed 3K, 5% free 4090K/4284K, paused 77ms, total 77ms
02-21 20:06:57.347: D/dalvikvm(4814): GC_CONCURRENT freed 5K, 5% free 4086K/4284K, paused 27ms+32ms, total 134ms
02-21 20:06:57.457: D/AndroidRuntime(4814): Shutting down VM
02-21 20:06:57.457: W/dalvikvm(4814): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
02-21 20:06:57.477: E/AndroidRuntime(4814): FATAL EXCEPTION: main
02-21 20:06:57.477: E/AndroidRuntime(4814): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.bihe.prayerbookletapp/org.bihe.prayerbookletapp.ExpListActivity}: java.lang.NullPointerException
02-21 20:06:57.477: E/AndroidRuntime(4814):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at android.os.Looper.loop(Looper.java:137)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at java.lang.reflect.Method.invoke(Method.java:511)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at dalvik.system.NativeStart.main(Native Method)
02-21 20:06:57.477: E/AndroidRuntime(4814): Caused by: java.lang.NullPointerException
02-21 20:06:57.477: E/AndroidRuntime(4814):     at org.bihe.database.DataSource.findAllPrayers(DataSource.java:163)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at org.bihe.prayerbookletapp.ExpListActivity.onCreate(ExpListActivity.java:65)
02-21 20:06:57.477: E/AndroidRuntime(4814):     at 

android.app.Activity.performCreate(Activity.java:5104)     02-21 20:06:57.477:E / AndroidRuntime(4814):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)     02-21 20:06:57.477:E / AndroidRuntime(4814):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)     02-21 20:06:57.477:E / AndroidRuntime(4814):... 11更多


和这里:02-21 20:06:57.477:E / AndroidRuntime(4814):at org.bihe.database.DataSource.findAllPrayers(DataSource.java:163)

它指的是Datasource类中的findAllPrayers()方法:

Here is my findAllPrayers() method which is in datasource class:

public List<Prayer> findAllPrayers()
{

    List<Prayer> prayerlist = new ArrayList<Prayer>();
    //
    Cursor cursor = database.query(DBOpenHelper.TABLE_PRAYER,
            allPrayerColumns, null, null, null, null, null);
    //
    Log.i(LOGTAG, "Child Cursor returned " + cursor.getCount() + " row(s).");

    if (cursor.getCount() > 0)
    {

        while (cursor.moveToNext())
        {

            Prayer p = new Prayer();
            p.setPrayer_name(cursor.getString(cursor
                    .getColumnIndex(DBOpenHelper.COLUMN_PRAYER_NAME)));

            p.setPrayer_ID(cursor.getLong(cursor
                    .getColumnIndex(DBOpenHelper.COLUMN_PRAYER_ID)));
            p.setPrayer(cursor.getString(cursor
                    .getColumnIndex(DBOpenHelper.COLUMN_PRAYER)));

            prayerlist.add(p);
        }
    }

    return prayerlist;

}

0 个答案:

没有答案