I am trying to set up a menu with drop down children. I have created a regular table for menu items, and a set / array / list (tried each) into which child items will get moved to make it easier to render via the JSP. However, when i try to use .add() to add items into the list of children, I receive a java.lang.NullPointerException and it goes no further.
My system is set up using Spring / Hibernate, and everything works except this.
Model (excluding irrelevant fields and getters / setters):
public class Menu {
@Transient
private Set<Menu> pageChildren;
@Column(name = "pageParent")
private Integer pageParent;
@Column(name = "pageId")
private Integer pageId;
...
And the necessary snippet from the controller:
List<Menu> menuItems = new ArrayList<Menu>();
menuItems = menuService.getAll();
for (Menu item: menuItems) {
if (item.getPageType() == 1 && item.getPageParent() > 0) {
menuItems.get(item.getPageParent() - 1).getPageChildren().add(item);
menuItems.remove(item);
}
}
The transient variable needed initializing, and the for loop was configured wrong.
Model (excluding irrelevant fields and getters / setters):
public class Menu {
@Transient
private Set<Menu> pageChildren = new HashSet<Menu>(); //CHANGED
@Column(name = "pageParent")
private Integer pageParent;
@Column(name = "pageId")
private Integer pageId;
...
And the necessary snippet from the controller:
List<Menu> menuItems = new ArrayList<Menu>();
menuItems = menuService.getAll();
for(int i=0; i<menuItems.size(); i++){ //CHANGED
Menu item = menuItems.get(i); //CHANGED
if (item.getPageType() == 1 && item.getPageParent() > 0) {
menuItems.get(item.getPageParent() - 1).getPageChildren().add(item);
menuItems.remove(item);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.