XML 字符串到 Java 对象 nullpointerexception
XML String to Java Object nullpoiterexception
错误:java.lang.NullPointerException:无法调用“java.util.List.iterator()”,因为“列表”为空
主要class:
try {
jaxbContext = JAXBContext.newInstance(Recipes.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Recipes rec = (Recipes) jaxbUnmarshaller.unmarshal(new StringReader(xml));
List <Recipe> list = rec.getRecipes();
for (Recipe re: list){
System.out.println(re.getId());
}
} catch (JAXBException e) {
e.printStackTrace();
}
食谱class:
@XmlRootElement(name = "results")
public class Recipe {
private int id;
private String title;
private Image image;
public Recipe(){}
public Recipe(int id, String title, Image image){
this.id = id;
this.title = title;
this.image = image;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
食谱class:
@XmlRootElement
public class Recipes {
private List<Recipe> recipes;
public Recipes(){}
public Recipes(List<Recipe> recipes){
this.recipes = recipes;
}
@XmlElement
public List<Recipe> getRecipes() {
return recipes;
}
public void setRecipes(List<Recipe> recipes) {
this.recipes = recipes;
}
}
我正在尝试管理一个 xml 字符串,它可以转换为对象数组。
一个错误给我这个列表是空的消息,我想,但我不明白为什么,谢谢你的帮助。
看起来 unmarshal
和 jaxbUnmarshaller
returns 对您稍后使用的列表来说是空的。你能不能用调试检查它 returns 是什么?
此外,如果您的 class 是使用 no-args 构造函数。您可能想在该空构造函数中键入以下代码:
recipes = new ArrayList<>();
错误:java.lang.NullPointerException:无法调用“java.util.List.iterator()”,因为“列表”为空
主要class:
try {
jaxbContext = JAXBContext.newInstance(Recipes.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Recipes rec = (Recipes) jaxbUnmarshaller.unmarshal(new StringReader(xml));
List <Recipe> list = rec.getRecipes();
for (Recipe re: list){
System.out.println(re.getId());
}
} catch (JAXBException e) {
e.printStackTrace();
}
食谱class:
@XmlRootElement(name = "results")
public class Recipe {
private int id;
private String title;
private Image image;
public Recipe(){}
public Recipe(int id, String title, Image image){
this.id = id;
this.title = title;
this.image = image;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
食谱class:
@XmlRootElement
public class Recipes {
private List<Recipe> recipes;
public Recipes(){}
public Recipes(List<Recipe> recipes){
this.recipes = recipes;
}
@XmlElement
public List<Recipe> getRecipes() {
return recipes;
}
public void setRecipes(List<Recipe> recipes) {
this.recipes = recipes;
}
}
我正在尝试管理一个 xml 字符串,它可以转换为对象数组。 一个错误给我这个列表是空的消息,我想,但我不明白为什么,谢谢你的帮助。
看起来 unmarshal
和 jaxbUnmarshaller
returns 对您稍后使用的列表来说是空的。你能不能用调试检查它 returns 是什么?
此外,如果您的 class 是使用 no-args 构造函数。您可能想在该空构造函数中键入以下代码:
recipes = new ArrayList<>();