XML字符串到Java对象NullPoInterException

发布于 2025-01-21 09:36:33 字数 1608 浏览 1 评论 0原文

错误: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日 2025-01-28 09:36:33

unmarshaller 接口,该操作首先使用no-args构造函数创建POJO的默认实例,然后从XML设置每个属性。

在您的情况下,配方类中的No-Args构造函数不会初始化list> list属性配方 ,将其留给null。因此,当您尝试迭代Unmarshalled对象rec的元素时,该操作将抛出nullpoInterException

您可以通过以下方式修复您的代码:

  1. 在NO-ARGS构造函数中初始化list
@XmlRootElement
public class Recipes {
    private List<Recipe> recipes;

    public Recipes(){
        this.recipes = new ArrayList();
    }

    public Recipes(List<Recipe> recipes){
        this.recipes = recipes;
    }

    ...
}
  1. 在声明期间初始化列表
@XmlRootElement
public class Recipes {
    private List<Recipe> recipes = new ArrayList();

    public Recipes(){
        this.recipes;
    }

    public Recipes(List<Recipe> recipes){
        this.recipes = recipes;
    }

    ...
}

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 the List attribute recipes, leaving it to null. Therefore, when you attempt to iterate through the elements of the unmarshalled object rec, the operation throws a NUllPointerException.

You could fix your code by:

  1. Initializing the List within the no-args constructor.
@XmlRootElement
public class Recipes {
    private List<Recipe> recipes;

    public Recipes(){
        this.recipes = new ArrayList();
    }

    public Recipes(List<Recipe> recipes){
        this.recipes = recipes;
    }

    ...
}
  1. Initializing the List during declaration.
@XmlRootElement
public class Recipes {
    private List<Recipe> recipes = new ArrayList();

    public Recipes(){
        this.recipes;
    }

    public Recipes(List<Recipe> recipes){
        this.recipes = recipes;
    }

    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文