0

I'm having troubles writing to memory, and from there accessing it. I believe I am writing it correctly, but when reading it, I'm getting a null pointer exception at this section of code:

fis = new FileInputStream(getFilesDir() + "/" + "runeInfo");

in the read portion of my code. I haven't dealt with saving data to a local file before, so I'm really not sure what I could be doing wrong. If anyone could point me in the right direction, I'd appreciate it.

public class GetRunes extends AsyncTask<String, String, String> {

        boolean runesCached = false;

        protected String doInBackground(String[] runeId) {
            String url = "https://prod.api.pvp.net/api/lol/static-data/" + region + "/v1.2/rune?api_key=" + api_key;
            JSONParser jsonParser = new JSONParser();
            JSONObject runeInfo = jsonParser.getJSONFromUrl(url);
            String jsonString = runeInfo.toString();
            String readJson = null;

            if(!runesCached) {
                Log.d("Cache", "Caching File");

                try {
                    FileWriter fstream;
                    BufferedWriter out;

                    fstream = new FileWriter(getFilesDir() + "/" + "runeInfo");
                    out = new BufferedWriter(fstream);
                    out.write(String.valueOf(jsonString.getBytes()));
                    out.close();

                } catch (Exception e){}
                Log.d("Cache", "Cache Complete");
                runesCached = true;
            }

            String name = null;
            try {
                FileInputStream fis;

                fis = new FileInputStream(getFilesDir() + "/" + "runeInfo");

                fis.read(readJson.getBytes());
                JSONObject storedJson = new JSONObject(readJson);
                Log.d("Stored JSON", "" + storedJson);
                JSONObject idJson = storedJson.getJSONObject("data");
                JSONObject single = idJson.getJSONObject(runeId[0]);


                try {
                    name = single.getString("name");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return name;
        }
    }
}
Nate
  • 392
  • 3
  • 5
  • 17
  • how do you know you're writing it correctly? you have all the writing in a try/catch block, and yet you're throwing away any potential Exceptions without handling them. – panini Apr 29 '14 at 02:27
  • @panini You're right, I'm really not sure. It wasn't throwing an error and I just overlooked it. Looking in my phones files, I don't see anything with my file name. Does my code look right for writing to memory? – Nate Apr 29 '14 at 02:31
  • So you changed } catch (Exception e){} to output errors? And did it show anything in logcat? – nasch Apr 29 '14 at 04:38
  • @nasch Yes, I have changed it, and I am getting a NPE at fstream = new FileWriter(getFilesDir() + "/" + "runeInfo"); that the catch was suppressing. – Nate Apr 29 '14 at 05:22
  • Output getFilesDir () and see if it looks right. – nasch Apr 30 '14 at 01:02

1 Answers1

1

Storing and retrieving information from files on the internal storage of an android device can be found here: Storage Options | Android Developer

Luckily a lot of the work is shown here: Parsing JSON from InputStream

So to read a JSON Object from an internal file:

String fileName = "Enter your file name here";
FileInputStream fis = openFileInput(fileName); 

InputStreamReader isr = new InputStreamReader(fis);
BufferedReader streamReader = new BufferedReader(isr);

StringBuilder responseStrBuilder = new StringBuilder();

String inputStr;
while ((inputStr = streamReader.readLine()) != null)
    responseStrBuilder.append(inputStr);
JSONObject jsonobj = new JSONObject(responseStrBuilder.toString());
streamReader.close();
isr.close();
fis.close();

To write you also need to specify the file MODE, advised to use MODE_PRIVATE. If a file with the given name does not yet exist it will be created if possible, otherwise a FileNotFoundException will be thrown.

String fileName = "Enter your file name here";
FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
...
fos.close();
Community
  • 1
  • 1
vyncjob
  • 21
  • 5