Tree collapse and expand problem
Posted: 10 Sep 2019, 23:18
The application has an hierarchy relationship and is built by primefaces tree component. There are four types of tree nodes defined: Organization, Building, Floor and Room. The tree is built dynamically whenever user clicks a type of tree node. For e.g., if Organization type node is clicked, all of the Children nodes(Building types of nodes) is built by fetching data from database for a specific organization. When floor type of node is selected, all of the children nodes (Room type nodes) will be built. User can click expand or collapse arrows for each type of tree nodes except the room type of node.
The problem we encountered is that when user expand or collapse different types of nodes many times, the application will throw exception below. If there is not much expand or collapse actions, the tree data can be displayed correctly. It would be great if you can help shed some lights on this problem. Thanks in advance.
xhtml file:
Session Bean:
Exception:
The problem we encountered is that when user expand or collapse different types of nodes many times, the application will throw exception below. If there is not much expand or collapse actions, the tree data can be displayed correctly. It would be great if you can help shed some lights on this problem. Thanks in advance.
Code: Select all
Hierarchy Relationship Structure:
1.0 Organization
1.1 Building 1
1.1.1 4th Floor
1.1.1.1. Room 1
1.1.1.2 Room 2
1.2 Building 2
Code: Select all
<p:tree selectionMode="single" selection="#{deptFacilitiesTree.selectedNode}" value="#{deptFacilitiesTree.root}"
var="node" id="treePrime" cache="true" dynamic="true" >
<p:ajax event="select" listener="#{facilitiesController.onNodeSelect}"/>
<p:ajax event="expand" listener="#{facilitiesController.onNodeExpand}" />
<p:ajax event="collapse" listener="#{facilitiesController.onNodeCollapse}" />
<p:ajax event="unselect" listener="#{facilitiesController.onNodeUnselect}" />
<p:treeNode type="organization" icon="#{node.survey.treeIcon}" >
<h:outputText value="#{node.departmentName}" escape="false"/>
</p:treeNode>
<p:treeNode type="building" icon="#{node.treeIcon}">
<h:outputText value="#{node.bldg}" escape="false"/> <h:outputText value="#{node.description}" escape="false"/>
</p:treeNode>
<p:treeNode type="floor" icon="#{node.treeIcon}">
<h:outputText value="#{node.description}" escape="false"/>
</p:treeNode>
<p:treeNode type="room" icon="#{node.treeIcon}">
<h:outputText value="#{node.locationCode}"/>, <h:outputText value="#{node.locTypeDescription}" escape="false"/>
</p:treeNode>
</p:tree>
Code: Select all
@ManagedBean(name = "facilitiesController")
@SessionScoped
public class FacilitiesController implements Serializable {
private void onNodeSelect(TreeNode treeNode) {
String nodetype = treeNode.getType();
RequestContext context = RequestContext.getCurrentInstance();
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
stickyBoxInformation = "";
context.update("stickyBoxInformation");
if (nodetype.contentEquals("organization")) {
currentOrganizationNode = treeNode;
closeForm();
buildDepartmentInfoString();
determineSubmitStatus();
ReportController rc = (ReportController) FacesContext
.getCurrentInstance().getExternalContext().getSessionMap()
.get("reportController");
rc.calculateCountOfAssignees();
LOGGER.debug("Organization treeNode expanded: " + treeNode.isExpanded());
// Expand/collapse appropriately on node click
// if the survey isn't completed...
if(!PortalConstants.TREE_COMPLETE.equals(((Department)treeNode.getData()).getSurvey().getTreeIcon())) {
// Expand the current node and its parents
// Department
//SS--118
if (treeNode.isExpanded())
treeNode.setExpanded(false);
else
treeNode.setExpanded(true);
List<Building> buildings = new SurveyDataModel().getBuildingsForSurvey(((Department)treeNode.getData()).getSpaceSurvey());
// put the buildings on the survey
((Department)treeNode.getData()).getSurvey().setBuildingsAndIds(buildings);
// reset the children and make the tree nodes for the buildings
((DefaultTreeNode)treeNode).setChildren(new ArrayList<TreeNode>());
// get the buildings from the survey so they'll be sorted properly
for(Building building : ((Department)treeNode.getData()).getSurvey().getBuildings()) {
TreeNode buildingNode = new DefaultTreeNode("building", building, treeNode);
// add an empty "floor" so the expand/collapse arrow shows on the building
new DefaultTreeNode("floor", null, buildingNode);
}
}//end if
context.update("treePrime");
}
................
}
Exception:
9/10/19
2:14:27.000 PM Caused by: javax.faces.FacesException: java.lang.NullPointerException at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:273) at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:57) at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:927) at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78) ... 30 more Caused by: java.lang.NullPointerException at org.primefaces.component.tree.Tree.queueEvent(Tree.java:340) at org.primefaces.behavior.ajax.AjaxBehaviorRenderer.decode(AjaxBehaviorRenderer.java:47) at javax.faces.component.behavior.ClientBehaviorBase.decode(ClientBehaviorBase.java:132) at org.primefaces.renderkit.CoreRenderer.decodeBehaviors(CoreRenderer.java:553) at org.primefaces.component.tree.TreeRenderer.decode(TreeRenderer.java:62) at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:832) at org.primefaces.component.tree.Tree.processDecodes(Tree.java:405) at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:573) at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183) at org.primefaces.component.api.UITree.visitTree(UITree.java:738) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) at javax.faces.component.UIForm.visitTree(UIForm.java:362) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) at javax.faces.component.UIComponent.visitTree(UIComponent.java:1700) at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:403) at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:266) ... 33 more