-3

I am trying to call the following method which is supposed to parse a JSON file from another activity. How come Android Studio is showing me a error?

This is the first activity called FirstActivity.java

 public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);

        ReadJSON classToReadJSON = new ReadJSON();
        classToReadJSON.loadJSONFromAsset();
    }}

This is my second class ReadJSON.java:

public class ReadJSON extends AppCompatActivity{
    public String loadJSONFromAsset() {
            String json = null;
            try {
                InputStream is = getAssets().open("jsonfile.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;
        }
}

The code to parse the JSON file derives from Faizan's answer. Reading a json file in Android

Edit: Sorry for not having posted the error log, I am still pretty new to programming.

So this is the error log:

D/AndroidRuntime: Shutting down VM


                  --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.zeus.jsonstackoverflow, PID: 2805
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zeus.jsonstackoverflow/com.example.zeus.jsonstackoverflow.FirstActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                      at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
                      at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
                      at android.view.ContextThemeWrapper.getAssets(ContextThemeWrapper.java:116)
                      at com.example.zeus.jsonstackoverflow.ReadJSON.loadJSONFromAsset(ReadJSON.java:16)
                      at com.example.zeus.jsonstackoverflow.FirstActivity.onCreate(FirstActivity.java:14)
                      at android.app.Activity.performCreate(Activity.java:6662)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

I would highly appreciate any help on this matter.

Community
  • 1
  • 1
LoveCoding
  • 79
  • 2
  • 13

3 Answers3

1

You have to pass Context to the method if you are not using Activity try this :

 public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);

        ReadJSON classToReadJSON = new ReadJSON();
        classToReadJSON.loadJSONFromAsset(this);
    }
}

ReadJSON.class

public class ReadJSON {
public String loadJSONFromAsset(Context c) {
        String json = null;
        try {
            InputStream is = c.getAssets().open("jsonfile.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;
    }
}
vivek mahajan
  • 521
  • 3
  • 16
1

Quite a few problems/misconceptions in your code.

First, your ReadJSON doesn't need to extend AppCompatActivity as it's not an Activity. This is one way of doing it:

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);

        String myJsonString = loadJSONFromAsset();
    }

    public String loadJSONFromAsset() {
        String json = null;
        try {
            InputStream is = getAssets().open("jsonfile.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;
    }

}

Second, with the returned json you have to do something. I put it in the myJsonString variable and probably you will have to transform it into a JSONObject and parse it.

The crash appears because you are trying to .getAssets(...) from a null context. By putting the loadJSONFromAsset in your activity you will have a context.

Adrian Coman
  • 1,536
  • 17
  • 30
0

Thank you for your help so far everybody!

I could find out that it has to do with the following line.

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

Why is it not possible to call this line from my first activity?

LoveCoding
  • 79
  • 2
  • 13