Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to store a Json Response in a String onto a Text file as a Cache. But I am not able to parse it when I read it from the text file.

    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++;
        }   

This is the Log Message

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)
share|improve this question
    
Instead of doing Json parsing yourself, why don't you use a library like Jackson or Gson? –  RSenApps Aug 8 '13 at 19:40
    
all of my code currently uses jsonParsing, it is not possible for me atm to use any other library. Isn't there any way to fix this problem ? –  Ishan Khanna Aug 8 '13 at 19:43
    
sorry I don't know enough to help you, but I know that by doing your own Json parsing you are just asking for bugs down the road. Also by using an AsyncTask you will run into problems with lifecycle management. Even though it will take some effort I highly recommend you setup Robospice and Spring for Android which will save you a lot of trouble down the road... –  RSenApps Aug 8 '13 at 19:51
    
Is rosterJsonArray.getJSONObject(i) giving you back a non-null JSONObject every time? –  Justin Jasmann Aug 8 '13 at 19:55

2 Answers 2

The problem is this line:

JSONArray rosterJsonArray = new JSONArray(line);

Once your while loop completes, line is equal to null. So new JSONArray(line) throws a NullPointerException. Plus your while loop resets line every time, so line = line + "\n"; does nothing. You should create another String to put the lines in as you loop through your file.

share|improve this answer

try changing this line

String line = null;

to this

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

also initialize it outside the the try block (make it global or local inside the function

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.