Android -getFileDir()导致NullPointer异常

时间:2016-12-24 18:57:26

标签: java android nullpointerexception android-sqlite

我在这个项目中遇到了很大的问题。 在这个项目中,我想读取一个db数据变成一个SQLite。

但也发生了错误。

我将db数据(last.db)放在src→main→assets文件夹中。

Process: net.macdidi.lasttest, PID: 4829
              java.lang.ExceptionInInitializerError
                  at net.macdidi.lasttest.MainActivity.onCreate(MainActivity.java:20)
                  at android.app.Activity.performCreate(Activity.java:6262)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2458)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2565)
                  at android.app.ActivityThread.access$900(ActivityThread.java:150)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1395)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:168)
                  at android.app.ActivityThread.main(ActivityThread.java:5821)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getFilesDir()' on a null object reference
                  at net.macdidi.lasttest.DatabaseHelper.<clinit>(SQLiteOpenHelper.java:18)
                  at net.macdidi.lasttest.MainActivity.onCreate(MainActivity.java:20) 
                  at android.app.Activity.performCreate(Activity.java:6262) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2458) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2565) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:150) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1395) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:168) 
                  at android.app.ActivityThread.main(ActivityThread.java:5821) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 

这是我的代码

class DatabaseHelper extends SQLiteOpenHelper {
private static Context context;
private static String DB_PATH = context.getFilesDir().getAbsolutePath();
private static String DB_NAME = "last.db";

private final Context mCtx;
public DatabaseHelper(Context context) {
    super(context, DB_NAME, null, 1);
     this.mCtx = context;
}
public boolean createDatabase() {
    boolean dbExist = checkDatabase();
    this.getReadableDatabase();
    if (dbExist == false) {
        if (copyDatabase() == false) {
            return false;
        }
    }
    return true;
}
private boolean checkDatabase() {
    SQLiteDatabase checkDB = null;
    String dbpath = DB_PATH + DB_NAME;
    try {
        checkDB = SQLiteDatabase.openDatabase(dbpath,
                null, SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        return false;
    }
    if (checkDB != null) {
        checkDB.close();
        return true;
    }
    return false;
}
private boolean copyDatabase() {
    try {
        InputStream input = mCtx.getAssets().open(DB_NAME);
        this.getReadableDatabase();
        String outFileName = DB_PATH + DB_NAME;
        OutputStream output =
                new FileOutputStream(outFileName);
        byte [] buffer = new byte[1024];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
        return false;
    }
    return true;
}
public void onCreate(SQLiteDatabase db) {
}
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
}

}

我非常希望有人可以帮我解决这个大问题。

1 个答案:

答案 0 :(得分:0)

private static String DB_PATH = context.getFilesDir().getAbsolutePath();

首先,此时contextnull

其次,不要尝试从字段初始值设定项中调用从Activity继承的方法。将该工作推迟到onCreate(),通常在super.onCreate()之后。

所以用以下代码替换该行:

private static String DB_PATH;

并在onCreate()中添加:

DB_PATH=getFilesDir().getAbsolutePath();

或者,更好的是,删除所有这些代码和use SQLiteAssetHelper