I'm trying to open a Json file which is in the raw folder inside res My file structure
But it fails with every method I have tried, I just need the string since I can parse Json
Strings
already.
This is my Json
reader
public class Parser extends Activity {
public String getStringFromJson(String path) {
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(path)));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
It fails in the line:
br = new BufferedReader(new InputStreamReader(getAssets().open(path)));
And this is what logcat said:
02-03 10:44:13.440 2374-2374/com.example.ealcazar.kolibry E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ealcazar.kolibry/com.example.ealcazar.kolibry.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.BufferedReader.close()' on a null object reference
is there an easier way to read a Json
? what Path should I use when trying to access my file in the RAW folder
?
Thank you very much.