java - How to access a String array initialized in one method from another method? -
i have simple requirement have string array declare @ class level can use anywhere in class.
string image_url[];//declared outside of methods //initailzed image_url[] 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 use image_url array in following method as:-
private arraylist preparedata() { arraylist android_version = new arraylist<>(); (int = 0; < 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 run code app crash's null pointer exception @ line:-
magazineversion.setmagazine_image_url(image_url[i]);
this code 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 new programming or suggestion appreciated.thank you.
you array not initialized think that's why giving null pointer exception. try debug why array null. code snippet below execute scenario.
public class abc { string image_url[]; public void abc(){ image_url = new string[] {"10","20","30"}; } public static void main(string[] args) { abc = new abc(); a.abc(); system.out.println(a.image_url[0]+" "+a.image_url[1]+" "+a.image_url[2]); } //output - 10 20 30 }
Comments
Post a Comment