4

I upgraded from Glassfish 3 to 4 and now my JSPs are throwing NPEs. I haven't changed any code so it must be a difference in Glassfish. JSP code that used to look like this:

<c:when test="${invoke}">

Now gives me a NPE unless I change it to this:

<c:when test="${not empty invoke && invoke}">

I'm wondering why this change is necessary. Can someone tell me what caused this? Why did it used to work before and no longer works?

José Andias
  • 1,894
  • 2
  • 29
  • 31
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356

2 Answers2

1

Expression Language 3 is apparently part of JEE7, so you might check the specification. After some more searching around, I found this answer, which indicates that EL 3.0 changed its default behavior compared to 2.2. If you can change the default coercion behavior to match what it was before, your problem would go away. I'm not sure how to do this in Glassfish. Otherwise, you might consider switching to boolean primitives instead of Boolean objects.

The default coercion for nulls to non-primitive types (except String) returns nulls. For instance, a null coerced to Boolean now returns a null, while a null coerced to boolean returns false.

Also, when complaining of NPEs, it is virtually always necessary to provide relevant parts of the stack trace. Knowing which class and method threw the exception is fairly important.

Community
  • 1
  • 1
ngreen
  • 1,559
  • 13
  • 22
  • The line numbers are to lines in a compiled JSP file. Do you think that would still help you? – Daniel Kaplan Mar 20 '14 at 03:55
  • That bit of information helps, yes. That would implicate the JSP compiler, not necessarily the EL implementation. Unless there's a nested exception, in which case that would be what I would need. Even a stack trace of generated code tells a story. – ngreen Mar 20 '14 at 12:54
1

I had the same nasty annoyance about Glassfish 4 throwing NPEs on that kind of (wannabe) EL boolean evaluations:

<c:if test="${couldNotExistVar}">

Usage scenario for couldNotExistVar:

  • if some condition
    • create a flag
  • inquire flag
  • inquire flag
  • ...

JSP + JSTL + EL example:

<c:if test="${ obj.someExpensiveCalculation }">
    <c:set var="couldNotExistVar" value="true" scope="request" />
</c:if>

As a solution I found best for me to add this first line:

<c:set var="imSureExistsVar" value="false" scope="request" />
<c:if test="${ obj.someExpensiveCalculation }">
    <c:set var="imSureExistsVar" value="true" scope="request" />
</c:if>

Disadvantage:

  • When using this kind of pattern it's easy to forget to include that new extra line.

Be aware that the NPEs could only be detectable out of development environment if one uses a different (and even with the same Java EE version) application server that does not respond as Glassfish 4.

José Andias
  • 1,894
  • 2
  • 29
  • 31