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.
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
}
您的数组为null,并且如果您尝试访问null数组的元素,它将抛出一个null指针,而不是ArrayOutOfBound。
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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.