aspose file tools*
The moose likes Servlets and the fly likes null check Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "null check" Watch "null check" New topic
Author

null check

Bart Wilson
Greenhorn

Joined: Aug 01, 2001
Posts: 17
Please help me translate this shortcut code. I keep getting a null pointer exception. How do I check for a null pointer for a boolean?
<%=thisObject.isRequired()? "YES" : "NO"%>
Thank you for any help.
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
thisObject.isRequired()!= null
Simon Brown
sharp shooter, and author
Ranch Hand

Joined: May 10, 2000
Posts: 1913
    
    6
Originally posted by Bart Wilson:
Please help me translate this shortcut code. I keep getting a null pointer exception. How do I check for a null pointer for a boolean?
<%=thisObject.isRequired()? "YES" : "NO"%>
Thank you for any help.

I would imagine that the NullPointerException you are getting is because the reference to "thisObject" is null. In this case, you could do something like the following to guard yourself against it.
<%=(thisObject != null && thisObject.isRequired())? "YES" : "NO"%>
If "thisObject" is null, you'll get "NO" instead of the expression causing a NPE. Otherwise you'll get the behaviour as before.
As an aside, maybe it's worth considering custom tags as a way of wrapping up this type of functionality. :-)
Hope that helps...
Simon
p.s. it could also be that the isRequired() method is falling over somewhere but without seeing the code and/or the stack trace it's hard to tell
Rishi Singh
Ranch Hand

Joined: Dec 09, 2000
Posts: 321
as rightly mentioned by simon, u should check the thisObject for null , if its not then this should work.
<%if (thisObject!=null){
%>
<%=thisObject.isRequired()? "YES" : "NO"%>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: null check