1

Json 応答を文字列でテキスト ファイルにキャッシュとして保存しようとしています。しかし、テキストファイルから読み取ったときに解析できません。

    try {
        BufferedReader reader = new BufferedReader(new FileReader(Environment.getExternalStorageDirectory()
                 +File.separator
                 +"myDirectory" //folder name
                 +File.separator
                 +"roster.txt"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            Log.i("School FTB Cache", line);
            line = line + "\n";
        }

    JSONArray rosterJsonArray = new JSONArray(line);
    Log.i("School FTB Cache", rosterJsonArray.toString());
    JSONObject jObject = null;
    Roster roster = new Roster();
    rosterList = new ArrayList<Roster>(rosterJsonArray.length());
    int i=0;

        while(i<rosterJsonArray.length()){
            roster = new Roster();
            jObject = rosterJsonArray.getJSONObject(i);
            Log.e("roster", jObject.toString());
            roster.setId(Integer.valueOf(jObject.getString("id")));
            roster.setName(jObject.getString("name"));
            roster.setPosition(jObject.getString("position"));
            rosterList.add(i, roster);
            i++;
        }   

これはログメッセージです

08-09 01:03:09.703: I/School FTB Cache(18328): [{"position":"DB","id":"1","name":"Ne'Quan Phillips"},{"position":"DB","id":"2","name":"Tony Grimes"},{"position":"LB","id":"3","name":"TJ Taimatuia"},{"position":"LB","id":"4","name":"Steven Lakalaka"},{"position":"WR","id":"5","name":"Billy Ray StutzMaan"}]
08-09 01:03:09.703: W/System.err(18328): java.lang.NullPointerException
08-09 01:03:09.757: W/System.err(18328):    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
08-09 01:03:09.757: W/System.err(18328):    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
08-09 01:03:09.773: W/System.err(18328):    at org.json.JSONTokener.nextValue(JSONTokener.java:90)
08-09 01:03:09.773: W/System.err(18328):    at org.json.JSONArray.<init>(JSONArray.java:87)
08-09 01:03:09.773: W/System.err(18328):    at org.json.JSONArray.<init>(JSONArray.java:103)
08-09 01:03:09.773: W/System.err(18328):    at com.boilingstocks.schoolftb.RosterActivity$RosterUpdaterFromCache.doInBackground(RosterActivity.java:135)
08-09 01:03:09.773: W/System.err(18328):    at com.boilingstocks.schoolftb.RosterActivity$RosterUpdaterFromCache.doInBackground(RosterActivity.java:1)
08-09 01:03:09.781: W/System.err(18328):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-09 01:03:09.781: W/System.err(18328):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-09 01:03:09.781: W/System.err(18328):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-09 01:03:09.781: W/System.err(18328):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-09 01:03:09.781: W/System.err(18328):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-09 01:03:09.796: W/System.err(18328):    at org.json.JSONTokener.nextValue(JSONTokener.java:90)
08-09 01:03:09.796: W/System.err(18328):    at org.json.JSONArray.<init>(JSONArray.java:87)
08-09 01:03:09.796: W/System.err(18328):    at org.json.JSONArray.<init>(JSONArray.java:103)
08-09 01:03:09.796: W/System.err(18328):    at com.boilingstocks.schoolftb.RosterActivity$RosterUpdaterFromCache.doInBackground(RosterActivity.java:135)
08-09 01:03:09.796: W/System.err(18328):    at com.boilingstocks.schoolftb.RosterActivity$RosterUpdaterFromCache.doInBackground(RosterActivity.java:1)
08-09 01:03:09.796: W/System.err(18328):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-09 01:03:09.796: W/System.err(18328):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-09 01:03:09.796: W/System.err(18328):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-09 01:03:09.796: W/System.err(18328):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-09 01:03:09.796: W/System.err(18328):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-09 01:03:09.796: W/System.err(18328):    at java.lang.Thread.run(Thread.java:1019)
08-09 01:03:09.812: W/System.err(18328):    at java.lang.Thread.run(Thread.java:1019)
4

2 に答える 2

-1

この行を変更してみてください

String line = null;

これに

String line; // or this //String line = "";

また、try ブロックの外側で初期化します (関数内でグローバルまたはローカルにします)。

于 2013-08-08T20:14:57.107 に答える