android nullpointerexception getapplicationcontext DbHelper错误

时间:2018-09-17 17:21:51

标签: java android-sqlite

我有问题。我是Android和Java的新手。 我尝试使Database Helper类在其中读取txt文件并插入数据库表中。

下面是错误列表:

func PgSql(user User, statement string)(output []string, err error) {

_, err = db.Query(&users, `SELECT * FROM standard_lookup WHERE pkid_ = 'STATE|AFKDZ'`)

}
我试图以几种不同的方式将InsertTable放在onCreate类中 这是dbhelper类

09-17 18:46:56.950 26633-26640/? E/zygote64: Failed sending reply to debugger: Broken pipe
        09-17 18:47:03.032 26633-26633/com.example.koziol.nukaang E/MemoryLeakMonitorManager: MemoryLeakMonitor.jar is not exist!
        09-17 18:47:03.035 26633-26633/com.example.koziol.nukaang E/Minikin: Could not get cmap table size!
        09-17 18:47:03.379 26633-26633/com.example.koziol.nukaang E/AndroidRuntime: FATAL EXCEPTION: main
            Process: com.example.koziol.nukaang, PID: 26633
            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.koziol.nukaang/com.example.koziol.nukaang.StartActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
                at android.app.ActivityThread.-wrap12(Unknown Source:0)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
                at android.os.Handler.dispatchMessage(Handler.java:108)
                at android.os.Looper.loop(Looper.java:166)
                at android.app.ActivityThread.main(ActivityThread.java:7425)
                at java.lang.reflect.Method.invoke(Native Method)
                at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
             **caused by: java.lang.nullpointerexception: attempt to invoke virtual method 'android.content.context android.content.context.getapplicationcontext()' on a null object reference**
                **at com.example.koziol.nukaang.DbHelper.onCreate(DbHelper.java:56)**
                at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:310)
                at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:206)
                at com.example.koziol.nukaang.DbHelper.<init>(DbHelper.java:47)
                at com.example.koziol.nukaang.StartActivity.onCreate(StartActivity.java:32)
                at android.app.Activity.performCreate(Activity.java:7372)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302) 
                at android.app.ActivityThread.-wrap12(Unknown Source:0) 
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891) 
                at android.os.Handler.dispatchMessage(Handler.java:108) 
                at android.os.Looper.loop(Looper.java:166) 
                at android.app.ActivityThread.main(ActivityThread.java:7425) 
                at java.lang.reflect.Method.invoke(Native Method) 
                at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 

1 个答案:

答案 0 :(得分:0)

public DbHelper(Context context) {
    super(context, DataBase_Name, null, DataBase_Version);
    db = getWritableDatabase();
    ctx = context; //Add this line...
}

此外,如果已经有Context引用ctx,则不确定是否需要调用getApplicationContext。

我的2美分...