0

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.

tmm
  • 7
  • 5
  • @Dan Thanks now it gives me no error, but System.out.println(re.getId()); return nothing – tmm Apr 14 '22 at 20:03
  • @Dan i tried to see if System.out.println("c"); returns the c and it dosen't. You know why it not enter in the for cycle? – tmm Apr 14 '22 at 20:28
  • because apparently the list does not contain any elements. This could be either because the xml does not contain any elements or because your unmarshall is not reading the xml properly. – Dan Apr 14 '22 at 20:30
  • @Dan I'm not sure but, you think that maybe is because i didn't pass the json to a string but directly to a xml string? – tmm Apr 14 '22 at 20:36
  • it could be either that or the file does not contain any data. – Dan Apr 14 '22 at 20:37
  • @Dan ok, I will try something, thks for the help. Bye – tmm Apr 14 '22 at 20:39
  • @Dan i did, but dosen't show because i don't have reputation – tmm Apr 14 '22 at 20:51
  • @Dan Because `list` is null actually. – user207421 Apr 15 '22 at 00:33

1 Answers1

-1

It seems like that the unmarshal with jaxbUnmarshaller returns null to that list you're using later on. Can you maybe check what it returns with the debug?

Plus, your Recipes class leaves the internal List of Recipes set to null if your class is instantiated with the no-args constructor. You might wanna type the following code within that empty constructor:

recipes = new ArrayList<>(); 
Dan
  • 3,647
  • 5
  • 20
  • 26