1

I have the following piece of code:

<h:outputText value="#{lecture.lectureName}" />

<c:forEach items="#{criterionController.getCriteriaForLecture(lecture)}" var="criterion">

     <h:outputText value="#{criterion.criterionName}"  />
     <h:commandLink value="Edit"/>
     <h:commandLink value="Delete"/>

</c:forEach>

The output text part is working perfectly and displays what it should display so this proves that the lecture object is set. However the for each tag gives a null pointer exception. When I debugged the code, I saw that the lecture object was taken as null when the method getCriteriaForLecture() was called.

How can this behaviour explained?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
interboy
  • 856
  • 1
  • 11
  • 25
  • Try to change `c:forEach` for `ui:repeat` – Luiggi Mendoza Jun 20 '12 at 21:31
  • It is the same, again I get a null pointer exception – interboy Jun 20 '12 at 21:48
  • is your managed bean Request or View scoped? Also, why are you sending the whole managed bean instead of a property? – Luiggi Mendoza Jun 20 '12 at 21:51
  • it is session scoped. No, that method returns a list of objects, where I plan to display some attributes in the view. Isn't this the way to be done? @LuiggiMendoza – interboy Jun 20 '12 at 21:52
  • Is the `c:foreach` inside a datatable or another container that iterates through a list? What I can see from your code is that you're using a function where you receive a managed bean (very odd, I've never worked in that way, instead passing an entity). – Luiggi Mendoza Jun 20 '12 at 22:36
  • It's been a WHILE since I did JSF, but I was under the impression you couldn't pass arguments to functions inside the #{} blocks. You could pass a key to a map, but not an argument to a function. – billjamesdev Jun 20 '12 at 23:26
  • 1
    @BillJames that was in JSF 1.x. In JSF 2 you can pass arguments when calling your managed bean methods in the EL. – Luiggi Mendoza Jun 21 '12 at 00:41
  • @LuiggiMendoza Thanks... I'm glad they finally fixed that, it was a horrific restriction. – billjamesdev Jun 21 '12 at 03:10
  • @Luigi: this is not specific to JSF version used, but to EL version used. You can just do the same in JSF 1.x when using the proper EL version, which is EL 2.2 (or "JBoss EL" when using EL 2.1). But this is after all not OP's concrete problem. His environment supports EL 2.2. – BalusC Jun 21 '12 at 15:34

1 Answers1

2

This can happen if the lecturer variable is in turn been set by a JSF iterating component such as <h:dataTable>, <ui:repeat>, etc or probably a <p:tabView>, based on your previous question.

A more detailed explanation of this behaviour can be found here: JSTL in JSF2 Facelets... makes sense? To the point, JSTL tags runs during building the view, not during rendering the view. The lecturer variable is in your particular case only available during rendering the view and is thus always null during building the view, when JSTL runs.

To solve it, use a normal JSF component like <ui:repeat> instead.

<ui:repeat value="#{criterionController.getCriteriaForLecture(lecture)}" var="criterion">
     <h:outputText value="#{criterion.criterionName}"  />
     <h:commandLink value="Edit"/>
     <h:commandLink value="Delete"/>
</ui:repeat>

Much better would be not doing business actions in getters at all. Just make the List<Criterion> a property of Lecture instead.

<ui:repeat value="#{lecture.criterions}" var="criterion">
     <h:outputText value="#{criterion.criterionName}"  />
     <h:commandLink value="Edit"/>
     <h:commandLink value="Delete"/>
</ui:repeat>

See also Why JSF calls getters multiple times

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555