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