2013-02-13 74 views
2

我搜索了所有通過相關的線程,我沒有成功,歡迎任何相關的答案。從sdcard讀取json文件返回 - 空指針異常

我有一個JSON數據作爲.json文件的SD卡.NewFolder文件夾內, 我想讀文件中的數據和分析,找到時間戳和要重寫,如果時間戳是不同的。

以下是我使用的代碼,我得到aBuffer中的數據和jdata_fromfile中的值,但我在解析時得到了nullPointerException。

myFile = new File(Environment.getExternalStorageDirectory() + "/.NewFolder/" +str.get(i)); 
if(myFile.exists()) 
{ 
    FileInputStream fIn = new FileInputStream(myFile); 
    BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn)); 
    String aDataRow = ""; 
    String aBuffer = ""; 
    while ((aDataRow = myReader.readLine()) != null) 
    { 
     aBuffer += aDataRow; 
    } 
    myReader.close(); 
    jdata_fromfile = new JSONObject(aBuffer); 

    String timestamp_fromfile = jdata_fromfile.getString(str_timestamp.get(i)); 
    timestamp_val_fromfile.add(timestamp_fromfile); 
} 
else 
{ 
    myFile.createNewFile(); 
    writedate_tofile(value_json.toString()); 
    timestamp_val_fromfile.add("xxx"); 
} 

而且還在於,當我打開記事本文件,它是所有中只顯示數據的一部分一行,當我複製到http://jsonformatter.curiousconcept.com

我得到的全部數據和一個有效的json。 我可以在記事本中看到剩餘的數據 - ctrl + end並按下回車鍵。

這是爲什麼,我在哪裏出錯?

這是我的錯誤 - 登錄

02-13 18:44:52.109: E/aBuffer value(7134): is {"Status":{"Itemlist":[{"x - Drinks":{"Details":[{"type":"","image":"","price":"","name":"Minerals"},{"type":"","image":"","price":"","name":"Milk Shakes"},{"type":"","image":"","price":"","name":"Milk"},{"type":"","image":"","price":"","name":"Mineral Water"},{"type":"","image":"","price":"","name":"Hot Beverages"},{"type":"","image":"","price":"","name":"Chocolate Muffin and Ice Cream"}, ........................................................................{"type":"","image":"","price":"","name":"Blu 
02-13 18:44:52.189: E/jdata_fromfile value(7134): is {"x Time_stamp":"2013-02-12 12:30:00","Status":{"Itemlist":[{"x - Sides":{"Details":[{"type":"","image":"","name":"Onion Rings (6)","price":""},{"type":"","image":"","name":"Sausage Portion (Minimum 2 per portion)","price":""},.................................................................................................................... ,"x - Burgers":{"Details":[{"type":"","image":"","name":"x 5oz Burger","price":""},{"type":"","image":"","name":"x 5oz Cheese Burger","price":""},{"type":"","image":"","name":"x 5oz with Bacon and Cheese","price":""},{"type":"","imag 
02-13 18:44:52.189: E/Reading from internal file - Exception e(7134): java.lang.NullPointerException 

在此先感謝。

+1

在哪一行你會得到NullPointerException?只需發佈堆棧跟蹤。 – svennergr 2013-02-13 13:00:36

+0

我正在使用一個try-catch上面的代碼中,我也得到:02-13 18:33:15.909:從內部文件E /閱讀 - 異常E(6632):顯示java.lang.NullPointerException – VIGNESH 2013-02-13 13:07:26

+1

張貼整個堆棧跟蹤你得到。 – svennergr 2013-02-13 13:08:31

回答

1

我不知道上面的代碼有什麼問題,但這裏是使用不同JSON庫的解決方案。

我在我的一個android應用程序中使用了下面的代碼,它完全適用於從android文件系統讀取任何json文件。我用的是json-simple.jar而不是使用Android的JSON庫,我使用json-simple.jarhttp://code.google.com/p/json-simple/

把上面的jar放到你的Android項目的libs文件夾中。並在.Manifest文件中設置以下權限。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

然後把follwing代碼放在單獨的線程中。

JSONParser parser = new JSONParser(); 
try { 
    String PATH = Environment.getExternalStorageDirectory() + "/Download/list.json"; 
    Object object = parser.parse(new FileReader(PATH));    
    JSONObject jsonObject = (JSONObject) object; 
    //Use JSONArray instead of JSONObject if json file contains array of JSONs 
    // 
    // Do anything with jsonObject 
    // 
} catch(Exception e) { 
    // TODO: handle exception 
} 

這裏我list.json文件是在位置/mnt/sdcard/Download

爲了參考here是使用以上在簡單的Java項目庫的例子。