0

I have a simple requirement I have a String array which I declare at the class level so that I can use it anywhere in the class.

 String image_url[];//Declared outside of any methods

//Initailzed image_url[] to img_url[]
public void showJSON(String json)
    {
        ParseJSON pj =  new ParseJSON(json);
        pj.parseJSON();

        image_url= ParseJSON.img_url;
Toast.makeText(DisplayMagazine.this, "Image loc"+image_url[1], Toast.LENGTH_SHORT).show();
    }

I want to use the image_url array in the following method as:-

 private ArrayList prepareData() {


        ArrayList android_version = new ArrayList<>();

        for (int i = 0; i < magazine_version_names.length; i++) {
            MagazineVersion magazineVersion = new MagazineVersion();
            magazineVersion.setMagazine_version_name(magazine_version_names[i]);
            magazineVersion.setMagazine_image_url(image_url[i]);
            android_version.add(magazineVersion);
        }
        return android_version;
    }

When I run this code the app crash's with a null pointer exception at this line:-

magazineVersion.setMagazine_image_url(image_url[i]);

This is the code for ParseJSON:-

public class ParseJSON
{
    public static String[] magversion;
    public static String[] img_url;
    public static String[] download_url;
    public static String[] sample_url;
    public static String[] status_dl;
    public static String[] status_sm;
    public static String[] device_id;

    public static final String JSON_ARRAY = "result";
    public static final String KEY_MAGVER = "magversion";
    public static final String KEY_IMGURL = "img_url";
    public static final String KEY_DOWNLOADURL = "download_url";
    public static final String KEY_SAMPLEURL = "sample_url";
    public static final String KEY_STATUS_DL ="status_dl";
    public static final String KEY_DEVICE_ID ="device_id";

    private JSONArray magazines = null;

    private String json;

    public ParseJSON(String json){
        this.json = json;
    }

    public void parseJSON(){
        JSONObject jsonObject=null;
        try {
            jsonObject = new JSONObject(json);
            magazines = jsonObject.getJSONArray(JSON_ARRAY);

            magversion = new String[magazines.length()];
            img_url = new String[magazines.length()];
            download_url = new String[magazines.length()];
            sample_url= new String[magazines.length()];
            status_dl= new String[magazines.length()];
            device_id= new String[magazines.length()];

            for(int i=0;i<magazines.length();i++){
                JSONObject jo = magazines.getJSONObject(i);
                magversion[i] = jo.getString(KEY_MAGVER);
                img_url[i] = jo.getString(KEY_IMGURL);
                download_url[i] = jo.getString(KEY_DOWNLOADURL);
                sample_url[i] = jo.getString(KEY_SAMPLEURL);
                status_dl[i] = jo.getString(KEY_STATUS_DL);
                device_id[i] = jo.getString(KEY_DEVICE_ID);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}

I am new to programming so any help or suggestion is appreciated.Thank you.

anup
  • 415
  • 9
  • 28
  • can you show `ParseJSON(String)` constructor? – Jordi Castilla Jul 28 '16 at 11:35
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – OH GOD SPIDERS Jul 28 '16 at 11:36
  • Hint: understanding what fields are, and how they relate to your classes is **absolute essential basic** stuff. If you don't know about such things, you better keep your fingers away from android programming. For now. The point is: don't try to learn java doing android programming. First learn java basics, then turn to Android. Anything else will cause a (probably short) series of very frustrating "why this not worky" experiences for you. Short, because you will soon run out of motivation. – GhostCat Jul 28 '16 at 11:37
  • @JordiCastilla please check my updated question I have added the ParseJson class this is what my constructor looks like :- public ParseJSON(String json){ this.json = json; } – anup Jul 28 '16 at 11:44

3 Answers3

1

You Array is not initialized so I think that's why its giving null pointer exception. Try to debug that why array is null. Code snippet below help you to execute the scenario.

public class Abc {

    String image_url[];

    public void abc(){
        image_url = new String[] {"10","20","30"};
    }       

    public static void main(String[] args) {

        Abc a = new Abc();
        a.abc();
        System.out.println(a.image_url[0]+" "+a.image_url[1]+" "+a.image_url[2]);
    }
//OutPut - 10 20 30 
}
Manoj Kumar
  • 350
  • 5
  • 15
0

Your array is null and if you try to access an element of null array, it will throw a null pointer not an ArrayOutOfBound.

Rupesh
  • 338
  • 7
  • 19
0

We need to see the calling code, order of calls is important in your case!

Are you sure you are calling prepareData() after calling showJSON(), not before?

M-Zaiady
  • 136
  • 9