File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null check

 
Bart Wilson
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thisObject.isRequired()!= null
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 321
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"%>
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic