0

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);
Harikrishnan
  • 7,765
  • 13
  • 62
  • 113

2 Answers2

1

Check this.webSitesArray. It looks like this variable is null.

Nickolai Astashonok
  • 2,878
  • 3
  • 20
  • 23
  • Just adding then this is visible in the bottom half of the stacktrace where is says "Caused by: java.lang.NullPointerException at info.androidhive.slidingmenu.json.WebSitesList.addToWebSitesArray(WebSitesList.java:51)" – yiati Jun 06 '14 at 18:44
-1

The correct way to load json data from asset folder is below

public String loadJSONFromAsset() {

String json = null;

try {

    InputStream is = getAssets().open("yourfilename.json");

    int size = is.available();

    byte[] buffer = new byte[size];

    is.read(buffer);

    is.close();

    json = new String(buffer, "UTF-8");


} catch (IOException ex) {
    ex.printStackTrace();
    return null;
}
return json;

}

  • Can you please explain how this code will work? My class doesn't extent activity or anything, so I can't simply call `getAssets()`. other than that, how does this differ from the code I posted? – Harikrishnan Jun 06 '14 at 13:19
  • you can access getAssets() class via getApplicationContext().getAssets() also – brightdev Jun 06 '14 at 13:23
  • You are not getting the point. `getApplicationContext()` can't be called from my class. It is a simple class that extends nothing Object. – Harikrishnan Jun 06 '14 at 13:42