This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JSP and the fly likes No NullPointerException in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "No NullPointerException in JSP" Watch "No NullPointerException in JSP" New topic
Author

No NullPointerException in JSP

Tomasz Nawrot
Greenhorn

Joined: Feb 19, 2012
Posts: 11
Hi,
I'm wondering why I don't get any NullPoinerException when using tags:

I created JSP file and place a tag USER into it like below:

<%@ taglib prefix="myTag" tagdir="/WEB-INF/tags/users" %>
<myTag:user user="${context.currentUser}" >

CurrentUser is taken from context but in DEBUG mode I saw that currentUser is NULL so we pass NULL value as parameter user to the tag.
My tag expect to retrieve user parameter and then it run method isAllowed on that object like follows:

user.tag
----------
<%@attribute name="user" required="true" type="com.mywork.User" %>
<c:if test="${user.allowed}">


I would expect to get here NullPointerException, but everything finshed without any problems. Why ?






Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Tomasz Nawrot wrote:
I would expect to get here NullPointerException, but everything finshed without any problems. Why ?

when you work with EL , you should keep in mind that EL operator suppresses null silently .
to check null you can use empty operator. one downside of it that it can also return true for non-null value like empty string or collections.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Tomasz Nawrot wrote:I would expect to get here NullPointerException, but everything finshed without any problems. Why ?

Why would you expect that? The EL Specification specifies how the EL behaves. Have you found a section in that document that states that a null pointer exception should occur in this situation?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: No NullPointerException in JSP