I have a class JSONParser and i am trying to parse a JSON file inside that. I have stored the
json file in the assets
folder. And I am trying to get it like this:
public class JSONParser {
Context context;
public JSONParser(Context context) {
this.context = context;
}
private String loadJSONFromAsset() {
String json = null;
try {
InputStream is = context.getAssets().open("websites.json");
// Other required codes
}
//rest of the code
}
//rest of code. I have a public function in this to return the parsed data to MainActivity
}
And in MainActivity
I have initiated this class like this:
public class MainActivity extends Activity {
private JSONParser jsonParser;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jsonParser = new JSONParser(getApplicationContext());
// Rest of code. I am calling the function in the JSONParser to get the object and all.
}
}
The app crashed. And I tried setting break point and found out that the json file failed to fetch. What is the issue here? What am I doing wrong? How to rectify this?
EDIT
Exception that I am getting in logcat:
06-06 18:42:41.724: E/AndroidRuntime(27040): FATAL EXCEPTION: main
06-06 18:42:41.724: E/AndroidRuntime(27040): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivity}: java.lang.NullPointerException
06-06 18:42:41.724: E/AndroidRuntime(27040): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
06-06 18:42:41.724: E/AndroidRuntime(27040): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
06-06 18:42:41.724: E/AndroidRuntime(27040): at android.app.ActivityThread.access$600(ActivityThread.java:162)
06-06 18:42:41.724: E/AndroidRuntime(27040): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
06-06 18:42:41.724: E/AndroidRuntime(27040): at android.os.Handler.dispatchMessage(Handler.java:107)
06-06 18:42:41.724: E/AndroidRuntime(27040): at android.os.Looper.loop(Looper.java:194)
06-06 18:42:41.724: E/AndroidRuntime(27040): at android.app.ActivityThread.main(ActivityThread.java:5371)
06-06 18:42:41.724: E/AndroidRuntime(27040): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 18:42:41.724: E/AndroidRuntime(27040): at java.lang.reflect.Method.invoke(Method.java:525)
06-06 18:42:41.724: E/AndroidRuntime(27040): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-06 18:42:41.724: E/AndroidRuntime(27040): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
06-06 18:42:41.724: E/AndroidRuntime(27040): at dalvik.system.NativeStart.main(Native Method)
06-06 18:42:41.724: E/AndroidRuntime(27040): Caused by: java.lang.NullPointerException
06-06 18:42:41.724: E/AndroidRuntime(27040): at info.androidhive.slidingmenu.json.WebSitesList.addToWebSitesArray(WebSitesList.java:51)
06-06 18:42:41.724: E/AndroidRuntime(27040): at info.androidhive.slidingmenu.json.JSONParser.getWebSitesList(JSONParser.java:43)
06-06 18:42:41.724: E/AndroidRuntime(27040): at info.androidhive.slidingmenu.MainActivity.onCreate(MainActivity.java:60)
06-06 18:42:41.724: E/AndroidRuntime(27040): at android.app.Activity.performCreate(Activity.java:5135)
06-06 18:42:41.724: E/AndroidRuntime(27040): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
06-06 18:42:41.724: E/AndroidRuntime(27040): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
06-06 18:42:41.724: E/AndroidRuntime(27040): ... 11 more
Line 60 in MainActivity.java
is where I call the function in JSONParser to return the parsed object. I tried by setting break points and I discovered that the actual problem is that the json file is never read to be parsed. In WebSitesList.java
line 51, I have this:
this.webSitesArray.add(aWebSiteDetail);