The following is the code I am using to read a JSON file stored in assets folder.
public class ReadJson extends Activity {
public String loadJSONFromAsset() {
String json1 = null;
try {
InputStream is = getAssets().open("jsonfile1.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json1 = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json1;
}
}
The app crashes and shows
"Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.getAssets()' on a null object reference" exception.
How to resolve this?