所以我的代码部分出了问题,我的sdcard将.csv文件导入到我的应用程序的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
at line(FCM.java:56)是db.beginTransaction(); 提前谢谢!
答案 0 :(得分:0)
由于NullPointerException
为db
,您在该行上获得了null
。您永远不会在上面显示的代码中初始化db
。您可以通过为null
分配非db
值来解决此问题。