Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
2 replies Latest Post - ‏2012-02-22T13:35:08Z by ChrisBSS
ChrisBSS
ChrisBSS
6 Posts
ACCEPTED ANSWER

Pinned topic JSF Tree (odc:tree) - null pointer exception on binding data to notes

‏2012-02-22T10:54:23Z |
Running WAS 6.0 using the following setup. Getting a null pointer exception as follows

22/02/12 10:49:13:622 GMT 00000063 SystemErr R javax.faces.el.EvaluationException: javax.faces.el.EvaluationException: Error getting property 'category3s' from bean of type uk.gov.renfrewshire.iar.tree.Category2Test: java.lang.NullPointerException

I am simply trying to build a 3 level tree structure. I know each level should return an array of its children. I can build this "manually" by creating the objects and then calling an "add child" method.

This works ok. I am now try to create the structure from some data from a database. The data structure appears to build properly.

The tree is configured as follows.

<odc:tree styleClass="tree" id="tree1" rootVisibleFlag="true"
enableSelect="true" value="#{treeroot}">

<odc:treeNodeAttr nodeLabel="Top Level"
className="uk.gov.renfrewshire.iar.tree.Root" id="treenodeattr1"
referenceName="category1"></odc:treeNodeAttr>
<odc:treeNodeAttr attributeName="name"
className="uk.gov.renfrewshire.iar.tree.Category1Test"
id="treenodeattr2" referenceName="category2s"></odc:treeNodeAttr>
<odc:treeNodeAttr attributeName="name"
className="uk.gov.renfrewshire.iar.tree.Category2Test"
id="treenodeattr3" referenceName="category3s"></odc:treeNodeAttr>
<odc:treeNodeAttr attributeName="name"
className="uk.gov.renfrewshire.iar.tree.Category3Test"
id="treenodeattr4"></odc:treeNodeAttr>
</odc:tree>

The "treeroot" class maps to the following java. This is where the category structure is setup.

package uk.gov.renfrewshire.iar.tree;

import java.util.*;

import pagecode.PageCodeBase;

import uk.gov.renfrewshire.iar.domain.LGCSCategory;
import uk.gov.renfrewshire.iar.services.category.CategoryService;

public class Root extends PageCodeBase {

private List category1 = new ArrayList();
public Root() {
System.out.println("root started");
CategoryService catLookup = (CategoryService) getManagedBean("catservice");
System.out.println("2");
catLookup = (CategoryService) getManagedBean("catservice");
System.out.println("3");
List results1 = (ArrayList) catLookup.getLevel1();
System.out.println("4");
for (int i = 0; i < results1.size(); i++) {
System.out.println("5");
LGCSCategory cat = (LGCSCategory) results1.get(i);
System.out.println("6");
Category1Test c1 = new Category1Test(new Integer(cat.getCatId()),
new String(cat.getName()));
this.category1.add(c1);
}

// List results2 = (ArrayList) catLookup.getLevel1();
System.out.println("7");
for (int i = 0; i < this.category1.size(); i++) {
System.out.println("8");
Category1Test curr1 = (Category1Test) this.category1.get(i);
List cat2list = (ArrayList) catLookup.getChildren(curr1.getId().intValue());
for(int j=0;j<cat2list.size();j++){
LGCSCategory cat2 = (LGCSCategory) cat2list.get(j) ;
if (cat2 != null) {
System.out.println("10");
Category2Test c2 = new Category2Test(
new Integer(cat2.getCatId()), new String(cat2.getName()));
System.out.println("11");

System.out.println("Getting level 3 with level 2 key of "+c2.getId());
List tlist = (ArrayList) catLookup.getChildren(c2.getId().intValue());
System.out.println("Size is "+tlist.size());
for(int x=0;x<tlist.size();x++){
System.out.println("3rd level "+x);
LGCSCategory cat3 = (LGCSCategory) tlist.get(x);
if(cat3!=null){
System.out.println("Cat 3 name = "+cat3.getName());
Category3Test c3 = new Category3Test(new Integer(cat3.getCatId()),new String(cat3.getName()));
c2.addCategory3(c3);
}
}

curr1.addCategory2(c2);
System.out.println("added category 2 to cat 1");
System.out.println("cat 2 length is"+curr1.getCategory2s().length);
}
}
//LGCSCategory cat = (LGCSCategory) catLookup.getLevel2(curr1.getId().intValue());
}
System.out.println("DONE ROOT");
// this.category2 = (ArrayList) catLookup.getLevel2(level1Id);

}

public void setCategory1(List category1) {
this.category1 = category1;
}

public Category1Test[] getCategory1() {
return (Category1Test[]) category1.toArray(new Category1Test[category1
.size()]);
}
}

Category classes are as follows

Category1Test
package uk.gov.renfrewshire.iar.tree;
import java.util.*;

public class Category1Test {
private Integer id;
private String name;
private List category2s;

public Category1Test(Integer id, String name) {
// TODO Auto-generated constructor stub
super();
this.id=id;
this.name=name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Category2Test[] getCategory2s() {
return (Category2Test[]) category2s.toArray(new Category2Testhttp://category2s.size());
}

public void setCategory2s(Category2Test[] category2) {
this.category2s = Arrays.asList(category2);
}

public void addCategory2(Category2Test cat){
if(category2s==null) category2s = new ArrayList();
category2s.add(cat);
}

}

Category2Test

package uk.gov.renfrewshire.iar.tree;
import java.util.*;

public class Category2Test {
private Integer id;
private String name;
private List category3s;

public Category2Test(Integer id, String name) {
// TODO Auto-generated constructor stub
super();
this.id=id;
this.name=name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Category3Test[] getCategory3s() {
return (Category3Test[]) category3s.toArray(new Category3Testhttp://category3s.size());
}

public void setCategory3s(Category3Test[] category3) {
this.category3s = Arrays.asList(category3);
}

public void addCategory3(Category3Test cat){
if(category3s==null) category3s = new ArrayList();
System.out.println("Adding acategogry 3");
category3s.add(cat);
}

}

Category3Test

package uk.gov.renfrewshire.iar.tree;

public class Category3Test {
private Integer id;
private String name;

public Category3Test(Integer id, String name) {
// TODO Auto-generated constructor stub
super();
this.id=id;
this.name=name;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Updated on 2012-02-22T13:35:08Z at 2012-02-22T13:35:08Z by ChrisBSS
  • yurykats
    yurykats
    2186 Posts
    ACCEPTED ANSWER

    Re: JSF Tree (odc:tree) - null pointer exception on binding data to notes

    ‏2012-02-22T12:53:32Z  in response to ChrisBSS
    Your category3s is not initialized (is null), so when you try to perform toArray on it in the getter method, it throws the NPE.
    • ChrisBSS
      ChrisBSS
      6 Posts
      ACCEPTED ANSWER

      Re: JSF Tree (odc:tree) - null pointer exception on binding data to notes

      ‏2012-02-22T13:35:08Z  in response to yurykats
      Fantastic - that was it. Can't believe I never spotted that. Anyway... thanks for your help - much appreciated...

      Next task is writing this stuff back to the database....eeeek!!!