• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how EL handles the null values

 
sriram kalakoti
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i have my exam, next monday and iam still confused with how the EL handles the null values

can anybody please clarify abt how El handles unknown values my confusion is when exception is thrown and when nothing is printed???


Thanks in advance
Sriram
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I can tell you two case where the exception will thrown.
1. When you are trying to access non-existent bean property.
2. When the attributes to list/arrays can not coerec to int.

hope it help

Thanks
 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try these cases,you will get a blank screen

1.Try to access an array index which is greater than the actual size

eg:${list["5"]}

2.Try to access a non-existent map key.

eg: ${m.C} //C is not a valid map key.

correct me if its wrong

Thanks
 
sriram kalakoti
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys

so if foo is an attribute but foo doesnot have a property 'name'
then ${foo.name} or ${foo[name]} will throw an exception right???

but in HFSJ pg 395 it says it will NOT throw an exception but just prints nothing.

need u people help again
Thanks
Sriram
 
Ernesto Leyva
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think EL trys to print as much as possible and is forgiving about null values so if you have a null object will print nothing a number not initialied will be zero and a boolean will be false.
 
K Bala
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EL handles null values in the following ways:

The logical operators treat a null value as false.

The arithmetic operators treat a null value as zero.

If you do division by zero, it returns infinity and does not throw any exception.

Hope this helps.
 
Frederic Esnault
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with K Bala's answer. Null values are treated in a very elegant way, without throwing any excpetion. The philosophy is that it's always better to show the user a blank where an information is expected than an ugly excpetion page.

Just a little point to add :

There is actually one case where EL throws an exception. When an EL function is defined, linked to a java class with a public static method to handle it. If this method throws an exception, the exception is wrapped into an ELException and thrown to the JSP error handling mechanism.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic