2014-03-30 148 views
0

我想從互聯網上下載一個.txt文件,然後讀取它並執行它。 我已成功下載,閱讀並執行單行查詢文件。 但問題是我不能從文件執行多行查詢。我做了一些研究,他們不適合我的需求,所以我想出了一個解決方案,但我不能找出一些錯誤。 我的想法是將.txt文件逐行轉換爲List,然後對List中的每個String執行sqlite中的查詢。 這裏是我的一些代碼:執行多行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; 
} 

問題是我堅持一個空指針異常。它說這條線是空的。 這裏是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

這可能是空指針異常的原因:

List<String> list = null; 

嘗試分配一個新的列表,而不是空的

+1

「+1」感謝的人!這是一個簡單的錯誤:D – KababChi