jeremyjjbrown
jeremyjjbrown

Reputation: 8009

Accessing a List autogenerated from XML Schema by Netbeans

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?

Upvotes: 0

Views: 236

Answers (1)

Tomer
Tomer

Reputation: 17940

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.

Upvotes: 1

Related Questions