0

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

我的代码:

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

简单地点:

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;

简单照片;

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 中执行所有这些操作,可能与此有关吗?或者我只是犯了一些我找不到的愚蠢错误。我之前在对象列表中使用过对象列表,但从未遇到过这个问题。我的错误在线

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

4

3 回答 3

2

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

public List<SimplePhotos> photos = new ArrayList<SimplePhotos>();
于 2012-07-27T12:35:04.707 回答
1

似乎 tempSv.photos 为空(尚未初始化(可能希望在您的构造函数中使用它))

于 2012-07-27T12:36:07.263 回答
0

您永远不会初始化 tempSv 的照片列表。您必须添加类似 tempSv .photos = new ArrayList();

于 2012-07-27T12:40:03.133 回答