简体   繁体   中英

Why am I getting NullPointerExceptions in my JSPs when I upgrade from Glassfish 3 to 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?

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 null s to non-primitive types (except String ) returns null s. For instance, a null coerced to Boolean now returns a null , while a null coerced to boolean returns false .

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM