0

I've auto generated code in Netbeans for an XML schema document into a package named jaxb. The root element Nutrition contains a child element named food that may appear many times.

<xsd:element name="food" maxOccurs="unbounded">

The Nutrition Object created by the autogeneration contains a protected List of Food objects.

protected List<Nutrition.Food> food;

When I try to add a Food object to the List with dot notation I can't access the list to add Food objects

Nutrition nutrition = objFactory.createNutrition();  //make a Nutrition object
Food food1 = objFactory.createNutritionFood();       // make a Food object
nutrition.food.add(food1);                           // add a Food object

Netbeans complains that "food has protected access in jaxb.Nutrition" I can't make the List public because it's auto generated. I've looked through the auto generated code for other methods with a reference to the List and there is only a getter that returns a copy of the list. How do I access the List to add a food object?

jeremyjjbrown
  • 7,772
  • 5
  • 43
  • 55

1 Answers1

1

The answer lies in your question, just use the getter to get the list and then add the object to it.

You are trying to access a property of the object nutrition which is protected so the way to access it is by using the get/set methods. This concept is known as encapsulation.

Tomer
  • 17,787
  • 15
  • 78
  • 137
  • I see your point. There is not setList() method because we want to deal with only one List per instantiated Nutrition instance, we do not want to pass a new List. – jeremyjjbrown Feb 06 '12 at 19:08