我想將一些數據放在自定義對象的列表中。我的主要自定義對象中的一項是包含兩個字符串的照片列表。但每次我嘗試添加照片項目我得到一個NullPointerException異常...NPE自定義對象中的(子)對象填充列表
我的代碼:
@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中使用過objectslists,但從來沒有這個問題。我的錯誤是在線
tempSv.photos.add(new SimplePhotos(large,medium));
你可以發佈堆棧跟蹤嗎?您也可以在測試之前在線上創建SimplePhotos,在嘗試設置它之前查看它是否爲空。 – 2012-07-27 12:27:45