Search code examples
javaandroidjsoninputstreamcalling-convention

Calling JSON from another activity JAVA


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.


Solution

  • 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;
        }
    }