whether to check null or not
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   pavan_kumar
Posted On:   Tuesday, October 10, 2006 03:17 AM

I have recieved a parameter rom request using getAttribute.my requirement is if that string equals to some string i need to forward the request to some jsp.in that situation is there any need to test that the string i got through request is a null or not.what happens if i apply equals method without checking whether that string is a null or not


Please use html tags to format code blocks.

Re: whether to check null or not

Posted By:   Tim_Frith  
Posted On:   Wednesday, October 11, 2006 05:48 PM

One simple thing you can do to avoid an exception but not explictly check for nulls is to flip things around in the equals() call.


Instead of:


String var=request.getParameter("somevar");
if(var.equals.("somevalue")) { forward to some page }


If you are checking equality with a string constant (such as "somevalue" above) you can write it like this:


String var=request.getParameter("somevar");
if("somevalue".equals.(var)) { forward to some page }

Since you know "somevalue" is never null you won't get an exception even if x is null.


Of course, if "somevalue" is another variable you will still have to explicitly check for null.

Re: whether to check null or not

Posted By:   WarnerJan_Veldhuis  
Posted On:   Tuesday, October 10, 2006 05:21 PM

You can just as well test it yourself:

String crap = null;
if( crap.equals("dung") ) {
//you will never get here
}


Oh, before I forget, please repeat after me:

I will never ever catch NullPointerExceptions. I will test for == null before calling methods on the object

I will never ever catch NullPointerExceptions. I will test for == null before calling methods on the object

I will never ever catch NullPointerExceptions. I will test for == null before calling methods on the object

I will never ever catch NullPointerExceptions. I will test for == null before calling methods on the object

Re: whether to check null or not

Posted By:   WarnerJan_Veldhuis  
Posted On:   Tuesday, October 10, 2006 05:18 PM

I thought it was very well explained here... Please continue in the same thread when you got more questions. Starting a new thread with the same question gives the previous posters the idea that their answer sucked. If they do, feel free to say so. In that case we can just ignore you :)

Re: whether to check null or not

Posted By:   pavan_kumar  
Posted On:   Tuesday, October 10, 2006 08:06 AM

It is always better to check the request variable for null.if u dont check the variable for null and try to apply the equals method there is a chance for occuring null pointer exception.Because u written acode like
String var=request.getParameter("somevar");
(if(var.equals.("somevalue")))
{
forward to some page
}
if u dont get the request variable a null will b there in the variable var.so by appling equals method using null results in null poiter exception. so it is always better to check a variable for null.
About | Sitemap | Contact