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.