如何从另一个方法访问在一个方法中初始化的String数组?

时间:2016-07-28 11:28:00

标签: java android arrays nullpointerexception

我有一个简单的要求我有一个String数组,我在类级别声明,以便我可以在类中的任何地方使用它。

 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();
    }

我想在以下方法中使用image_url数组: -

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

当我运行此代码时,应用程序崩溃,并在此行显示空指针异常: -

magazineVersion.setMagazine_image_url(image_url[i]);

这是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();
        }
    }

}

我是编程新手,所以感谢任何帮助或建议。谢谢。

3 个答案:

答案 0 :(得分:1)

您的数组未初始化,所以我认为这就是它给出空指针异常的原因。尝试调试为什么数组为null。下面的代码片段可帮助您执行方案。

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 
}

答案 1 :(得分:0)

您的数组为null,如果您尝试访问null数组的元素,它将抛出一个空指针而不是ArrayOutOfBound。

答案 2 :(得分:0)

我们需要查看调用代码,调用顺序对您来说很重要!

您是否确定在致电prepareData()后致电showJSON(),而不是之前?