0

因此,我在将 .csv 文件从 sdcard 导入应用程序中的 sqlite 数据库的代码部分遇到了问题。根据我的 LogCat 我的“db.beginTransaction();” 行因空指针而失败?我真的不知道如何解决这个问题,希望有人能帮助我!

这是我的代码将 csv 文件导入数据库的样子:

@Override
    public void onStart(){
        super.onStart();


        FileReader fr;
        try {
            fr = new FileReader(new File("mnt/sdcard/database/",
                    "fcmtest.csv"));
        } catch (FileNotFoundException e) {
            throw new Error("ERROR File Not Found");
        }
        BufferedReader br = new BufferedReader(fr);
        String data = "";
        String tableName ="MY_TABLE";
        String columns = "Serial Numbers,PartNumber,Result,Date,Time,Operator";
        String InsertString1 = "INSERT INTO " + tableName + " (" + columns + ") values(";
        String InsertString2 = ");";

        db.beginTransaction();
        try {
            while ((data = br.readLine()) != null) {
                StringBuilder sb = new StringBuilder(InsertString1);
                String[] sarray = data.split(",");
                sb.append("'" + sarray[0] + "',");
                sb.append(sarray[1] + "',");
                sb.append(sarray[2] + "',");
                sb.append(sarray[3] + "',");
                sb.append(sarray[4] + "',");
                sb.append(sarray[5] + "'");
                sb.append(InsertString2);
                db.execSQL(sb.toString());
            }
        } catch (SQLException e) {
            throw new Error("Error copying database");
        } catch (IOException e) {
            throw new Error("Error IOException");
        }
        db.setTransactionSuccessful();
        db.endTransaction();

    }

这是Logcat:

02-01 14:20:04.071: W/dalvikvm(7655): threadid=1: thread exiting with uncaught exception (group=0x41033930)
02-01 14:20:04.071: E/AndroidRuntime(7655): FATAL EXCEPTION: main
02-01 14:20:04.071: E/AndroidRuntime(7655): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.magna.magna/com.magna.magna.FCM}: java.lang.NullPointerException
02-01 14:20:04.071: E/AndroidRuntime(7655):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at android.os.Looper.loop(Looper.java:137)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at java.lang.reflect.Method.invokeNative(Native Method)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at java.lang.reflect.Method.invoke(Method.java:511)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at dalvik.system.NativeStart.main(Native Method)
02-01 14:20:04.071: E/AndroidRuntime(7655): Caused by: java.lang.NullPointerException
02-01 14:20:04.071: E/AndroidRuntime(7655):     at com.magna.magna.FCM.onStart(FCM.java:56)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at android.app.Activity.performStart(Activity.java:5114)
02-01 14:20:04.071: E/AndroidRuntime(7655):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
02-01 14:20:04.071: E/AndroidRuntime(7655):     ... 11 more

在 (FCM.java:56) 行是 db.beginTransaction(); 提前致谢!

4

1 回答 1

0

NullPointerException你在那条线上得到了 a ,因为dbis null。您永远不会在上面显示的代码中进行初始化db。您可以通过将非null值分配给 来修复它db

于 2013-02-01T19:49:09.023 回答