0

I have a h:dataTable retrieving a list of objects. For each of those objects I have to display some object information (obj.x, obj.y) but also a list of children linked to that obj.

I coded a function to retrieve the children. This function looks like this :

public List<ChildModel> getChildren(ObjectModel obj) {
    List<ChildModel> children = //...
    return children;
}

Since a want to display the children on a single line I thought about using a loop :

<h:dataTable value="objectList" var="obj">
  ...
  <c:forEach items="#{someBean.getChildren(obj)" var="child" >
     ...
  </c:forEach>
</h:dataTable>

But I get a java.lang.NullPointerException. When switching to a h:dataTable (like this) everything works fine :

<h:dataTable value="objectList" var="obj">
  ...
  <h:dataTable value="#{someBean.getChildren(obj)" var="child" >
     ...
  </h:dataTable>
</h:dataTable>

I also tried with a ui:repeat but I think (maybe wrongly) that it is not possible.

What I am missing?

ForguesR
  • 3,558
  • 1
  • 17
  • 39
  • The `ui:repeat` is correct one there since variable in `h:dataTable` is accessible only in render view time (it is the time where `ui:repeat` takes place so it works) in contrary to `c:forEach` which takes place in view build time. During view build time variable from render view time (your `h:dataTable` `var`) does not yet exist hence null. To learn more: http://stackoverflow.com/questions/29021036/why-can-cforeach-or-uirepeat-not-access-pdatatable-var – Geinmachi Oct 22 '15 at 20:46
  • @Geinmachi `ui:repeat` also gives me `java.lang.NullPointerException`. In the link you provided it looks like `c:forEach` will work only if it adds `p:column`. – ForguesR Oct 22 '15 at 22:01
  • @balusC I don't want to dynamically add columns. If I understand well the answer you provided, I have to replace the outer `h:dataTable` by a `ui:repeat` and do the "table presentation stuff" all by myself? – ForguesR Oct 22 '15 at 22:07
  • Just replace `` by ``. Their lifecycles are explained in the dupe and particularly the link therein http://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense – BalusC Oct 23 '15 at 07:56
  • @balusC It now works with `ui:repeat` but I had to do something weird : in `getChildren(ObjectModel obj)` I have to check if the `obj` parameter is `null` and, if so, I need to return an empty `List` array. It looks like the `h:dataTable` iterates on all the obj of the `objectList` **plus** one last time with a `null` value. – ForguesR Oct 23 '15 at 13:38
  • Business logic in getter methods is bad idea anyway. Better prepare exactly the model the view expects. See also http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times – BalusC Oct 23 '15 at 13:41

0 Answers0