Java.lang.NullPointerException in org.primefaces.component.menubar.MenubarRenderer.encodeSubmenuIcon

I am working on a line of dynamic strings. The contents of the menu bar should be updated when the control key is pressed.

Here is my very simple xhtml page

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:p="http://primefaces.org/ui"> <h:head> <title>CoSMo</title> </h:head> <h:body> #{simpleMenuBacking.init()} <h:form> <p:commandButton value="Submit" action="#{simpleMenuBacking.updateModel()}" update="mb1"/> <p:growl id="messages" showDetail="false"/> <p:menubar id="mb1" model="#{simpleMenuBacking.model}" /> </h:form> </h:body> 

And my (accordingly simple) bean support:

 @Named @SessionScoped public class SimpleMenuBacking implements Serializable { private DefaultMenuModel model; public void init() { Logger.getLogger(UserBacking.class.getName()).info("[SimpleMenuBacking.init()] Starting."); model = new DefaultMenuModel(); String myMenuEntry = "MENU ENTRY " + new Random().nextInt(); Logger.getLogger(UserBacking.class.getName()).info(myMenuEntry); model.addElement(new DefaultSubMenu(myMenuEntry)); } public SimpleMenuBacking() { } public DefaultMenuModel getModel() { Logger.getLogger(UserBacking.class.getName()).info("[SimpleMenuBacking.getModel()] Starting."); return model; } public void updateModel() { Logger.getLogger(UserBacking.class.getName()).info("[SimpleMenuBacking.updateModel()] Starting."); String myMenuEntry = "MENU ENTRY " + new Random().nextInt(); model.addElement(new DefaultSubMenu(myMenuEntry)); Logger.getLogger(UserBacking.class.getName()).info("[SimpleMenuBacking.updateModel()] Over."); } } 

Unfortunately, when I press the Command button, the line model is updated , but the changes do not appear on the menu bar. I realized that the following exception could almost certainly be the cause of this problem:

 SEVERE: Error Rendering View[/index.xhtml] java.lang.NullPointerException at org.primefaces.component.menubar.MenubarRenderer.encodeSubmenuIcon(MenubarRenderer.java:60) at org.primefaces.component.tieredmenu.TieredMenuRenderer.encodeSubmenu(TieredMenuRenderer.java:161) at org.primefaces.component.tieredmenu.TieredMenuRenderer.encodeElements(TieredMenuRenderer.java:127) at org.primefaces.component.tieredmenu.TieredMenuRenderer.encodeMenu(TieredMenuRenderer.java:78) at org.primefaces.component.menubar.MenubarRenderer.encodeMarkup(MenubarRenderer.java:50) at org.primefaces.component.menu.BaseMenuRenderer.encodeEnd(BaseMenuRenderer.java:108) at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863) at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:559) at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183) ... 

How is this caused and how can I solve it? I am ready to give more detailed information about the settings of my code and / or project. Thanks.

+5
source share
1 answer

Finally, I understood the problem.

This discussion explains that if the DefaultSubMenu objects do not have an identifier, the menu bar is not rendered. Just define an identifier for each DefaultSubMenu object, and you're done.

Alternatively, you can call

 menuModel.generateUniqueIds(); 

so that the model creates unique identifiers for you.

+7
source

Source: https://habr.com/ru/post/1200020/


All Articles