我正在尝试从互联网下载.txt文件,然后阅读并执行它。 我成功地下载,阅读和执行单行查询文件。 但问题是我无法从文件中执行多行查询。我做了一些研究,他们不适合我的需要,所以我提出了一个解决方案,但我无法弄清楚一些错误。 我的想法是将.txt文件逐行转换为List,然后为List中的每个String执行sqlite中的查询。 这是我的一些代码:
public void updateDB(View v){
new DownloadFileFromURL().execute(file_url);
File file = new File(Environment
.getExternalStorageDirectory().toString()
+ "/update.txt")
Log.i("UPDATE", "SQL DOWNLOADED");
while(start.equals(true)){
List<String> list = readUpdate2("update.txt");
for (String string : list) {
datasource.executeRaw(string);
Log.i("UPDATE", "EXEC SQL # : " + list.size());
}
file.delete();}}
public List<String> readUpdate2(String url){
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
List<String> list = null;
File file = new File(sdcard,url);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while((line = br.readLine()) != null) {
// this is where the error happens :
list.add(line);
Log.i("UPDATE", "LINE : " + line);
}
}
catch (IOException e) {
}
return list;
}
问题是我遇到了Nullpointer异常。它说这条线是空的。 这是logcat:
03-30 18:01:18.965: E/AndroidRuntime(12474): FATAL EXCEPTION: main
03-30 18:01:18.965: E/AndroidRuntime(12474): java.lang.IllegalStateException: Could not execute method of the activity
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.view.View$1.onClick(View.java:3838)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.view.View.performClick(View.java:4475)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.view.View$PerformClick.run(View.java:18786)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.os.Handler.handleCallback(Handler.java:730)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.os.Looper.loop(Looper.java:137)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.app.ActivityThread.main(ActivityThread.java:5493)
03-30 18:01:18.965: E/AndroidRuntime(12474): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 18:01:18.965: E/AndroidRuntime(12474): at java.lang.reflect.Method.invoke(Method.java:525)
03-30 18:01:18.965: E/AndroidRuntime(12474): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
03-30 18:01:18.965: E/AndroidRuntime(12474): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
03-30 18:01:18.965: E/AndroidRuntime(12474): at dalvik.system.NativeStart.main(Native Method)
03-30 18:01:18.965: E/AndroidRuntime(12474): Caused by: java.lang.reflect.InvocationTargetException
03-30 18:01:18.965: E/AndroidRuntime(12474): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 18:01:18.965: E/AndroidRuntime(12474): at java.lang.reflect.Method.invoke(Method.java:525)
03-30 18:01:18.965: E/AndroidRuntime(12474): at android.view.View$1.onClick(View.java:3833)
03-30 18:01:18.965: E/AndroidRuntime(12474): ... 11 more
03-30 18:01:18.965: E/AndroidRuntime(12474): Caused by: java.lang.NullPointerException
03-30 18:01:18.965: E/AndroidRuntime(12474): at com.meemarbashi.meemardictionary.UpdateActivity.readUpdate2(UpdateActivity.java:292)
03-30 18:01:18.965: E/AndroidRuntime(12474): at com.meemarbashi.meemardictionary.UpdateActivity.updateDB(UpdateActivity.java:223)
03-30 18:01:18.965: E/AndroidRuntime(12474): ... 14 more
所以基本上我没有从.txt文件中获取文本然后我无法向我的列表中添加任何内容。我该怎么办?
提前致谢
答案 0 :(得分:0)
这可能是空指针异常的原因:
List<String> list = null;
尝试分配新列表而不是null