NPE填充自定义对象中(子)对象的列表

时间:2012-07-27 12:25:22

标签: java android list nullpointerexception

我正在尝试将一些数据放入自定义对象列表中。我的主要自定义对象中的一个项目是包含两个字符串的照片列表。但每当我尝试添加照片项目时,我都会得到一个nullpointerexception ...

我的代码:

@Override
protected String doInBackground(Void... params) {
    String language =  Locale.getDefault().getISO3Language();
    AssetManager assetManager = getAssets();
    InputStream inputStream = null;
    try {
        inputStream = assetManager.open("MyJson.json");
    } catch (IOException e) {
        Log.e("tag", e.getMessage());
    }
    ObjectMapper objectMapper = new ObjectMapper();
    Log.i("tijdlog","start parsing" );
    try {

        List<JJsonResponse> jsonResponse = objectMapper.readValue(inputStream, new TypeReference<List<JJsonResponse>>() { });
        Log.i("tijdlog","ended json parsing" );
        final List<JJsonResponse> myGlobalVariable = jsonResponse;

        //Simple Venues maken, rest weggooien!
        List<SimpleVenue> sv = new ArrayList<SimpleVenue>();
        JJsonResponse e;
        int k =0; //indicator 1e SimpleVenue
        JVenueThemes jtheme;
        SimpleVenue tempSv;
        SimplePhotos tempPhoto;
        int selectedCounter;

        for(int i=0;i < jsonResponse.size() ;i++) {
            e = jsonResponse.get(i);
            if(e.venue.hidden == false) { //staat aan
                for(int j=0; j<e.venue.themes.size();j++) { //loop door alle themes
                    if (e.venue.themes.get(j).mobile == true) {  //als theme is true
                        jtheme = e.venue.themes.get(j);
                        sv.add(new SimpleVenue());
                        tempSv=sv.get(k);
                        tempSv.setId(k);
                        tempSv.setName(e.venue.name);
                        tempSv.setAdress(e.venue.address);
                        tempSv.setCity(e.venue.city);
                        tempSv.setPhone(e.venue.phone);
                        tempSv.setWebsite(e.venue.website);
                        tempSv.setFoursquare(e.venue.foursquare_link);
                        tempSv.setLatitude(e.venue.latitude);
                        tempSv.setLongitude(e.venue.longitude);
                        tempSv.setCategory(jtheme.icon);
                        tempSv.setIcon(jtheme.icon);
                        // language depending
                        if (language.equalsIgnoreCase("nld")){ //dutch
                            tempSv.setTip(e.venue.tip); 
                            tempSv.setTheme(jtheme.name); 
                        } else { //english
                             tempSv.setTip(e.venue.tip_en); 
                             tempSv.setTheme(jtheme.name_en); 
                        }
                        // put two (useless) photos items
                        for (int m = 0; m<2;m++) {
                            String large="pic1";
                            String medium="pic1";
                            tempSv.photos.add(new SimplePhotos(large,medium));                                    
                            k++;
                        }    
                    }
                }
            jsonResponse.remove(i); // 
            }
        }
    } catch (JsonParseException e) {
        // XXX Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // XXX Auto-generated catch block
        e.printStackTrace();
    }
    return language;
}

SimpleVenue:

public class SimpleVenue implements Serializable, Comparable<SimpleVenue>{
        /**
         * 
         */
    private static final long serialVersionUID = 1L;
    public int ID;
    public String name;
    public String category;
    public String address;
    public String city;
    public String tip;
    public String phone;
    public String website;
    public String foursquare;
    public float latitude;
    public float longtitude;
    public String theme;
    public String icon;
    public String exception;
    public List<SimplePhotos> photos;

SimplePhotos;

public class SimplePhotos implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public String medium;
    public String large;


    public SimplePhotos(String vmedium,String vlarge){
        medium = vmedium;
        large = vlarge;
    }

    public void setMedium(String vmedium){
        medium = vmedium;
    }

    public void setLarge(String vlarge){
        large = vlarge;
    }

    public String getMedium(){
        return medium;
    }

    public String getLarge(){
        return large;
    }

我不知道为什么这不起作用。我在背景中的AsyncTast中做了所有这些,可能是因为它与它有关吗? 或者我只是犯了一些我找不到的愚蠢错误。我以前在objectslist中使用了对象列表,但从未遇到过这个问题。我的错误就行了

  

tempSv.photos.add(new SimplePhotos(large,medium));

2 个答案:

答案 0 :(得分:2)

您尚未显示完整声明,但似乎您未初始化photos类中的SimpleVenue列表。您需要为该字段分配实现List接口的类的实例。例如:

public List<SimplePhotos> photos = new ArrayList<SimplePhotos>();

答案 1 :(得分:1)

似乎tempSv.photos为null(尚未初始化(可能在构造函数中想要这个))