XML字符串到Java对象NullPoInterException
错误:java.lang.nullpointerexception:无法调用“ java.util.list.iterator()”,因为“列表”是null
Main类:
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();
}
食谱类别:食谱类别:
@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;
}
食谱类:
@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字符串在一系列对象中。 我认为,一个错误给我一个消息,即列表是空的,但是我不明白为什么,感谢您的帮助。
Error: java.lang.NullPointerException: Cannot invoke "java.util.List.iterator()" because "list" is null
Main 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();
}
Recipe 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;
}
Recipes 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;
}
}
I'm trying to manage a xml string, that can be transformed in an array of objects.
An error give me this message that the list is empty, I think, but i don't understand why, thanks for helping.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用
unmarshaller
接口,该操作首先使用no-args构造函数创建POJO的默认实例,然后从XML设置每个属性。在您的情况下,
配方
类中的No-Args构造函数不会初始化list> list
属性配方
,将其留给null。因此,当您尝试迭代Unmarshalled对象rec
的元素时,该操作将抛出nullpoInterException
。您可以通过以下方式修复您的代码:
list
。列表
。When unmarshalling an XML with an implementation of the
Unmarshaller
interface, the operation first creates a default instance of the POJO with a no-args constructor, and then sets each property from the XML.In your case, the no-args constructor in the
Recipes
class does not initialize theList
attributerecipes
, leaving it to null. Therefore, when you attempt to iterate through the elements of the unmarshalled objectrec
, the operation throws aNUllPointerException
.You could fix your code by:
List
within the no-args constructor.List
during declaration.