• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bean Property-Dot Operator..

 
Sandya Bhaskara
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mock exam-chapter 8 question 17-
it says-

"if the . dot operator is used to access a bean property but the property does not exist,then a runtime exception is thrown."


but in chapter 9,page 447 (key points and gotchas- last point) it says EL expression by itself will NOT cause an exception if the property does not exist.so even though $(fooBean.notAProperty) won't cause an exception by itself(it just returns null) etc etc....

aren't these two contradictory???
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm not mistaken, somewhere in the forum, this query had been addressed before (or at least mentioned).

Anyway, I think the statement meant that if your bean is missing & you tried to use the dot operator to access any of its property, its gonna result in an exception. Because the bean doesn't exist, so that's gonna throw you a NullPointerException.

But, if the bean exists, but does not have that property, no exception will be thrown.

HTH
 
Gaurav Sahni
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear fellas,

Just could not digest what Lee is trying to say here.... the question still remains unanswered.

HFS pg. 424, question has one of the option which says clearly,

"If the .(dot) operator is used to access a bean property but the property does not exists, then a runtime exception is thrown."

and in the answers this is considered as "true" .


considering the forgiving nature of EL, if the bean is already present in the any scope, this should be false....
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an error in the book!!


It should NOT be marked as *correct*, so, give yourself a point if you left option "C" unchecked. You're absolutely right--"C" should not be marked as correct.

Sorry...
-Kathy
 
Gaurav Sahni
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot Kathy.
U always save the day for us.


Gaurav.
 
Gaurav Sahni
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused today again.

I tried to test this using tomcat5.0.
If a bean 'bar' exists, whic does NOT have a property 'foo' ,
then the following statement
${bar.foo}
give the following error:


javax.servlet.ServletException: Unable to find a value for "saa" in object of class "practise.wcd.person" using operator "." ......




This means that the above assumption
"If the .(dot) operator is used to access a bean property but the property does not exists, then a runtime exception is thrown."

is indeed correct. :roll:

Things are still not clear...

Gaurav.
 
Gaurav Sahni
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would greatly appreciate if anybody can clear this mud.
:roll: :roll: :roll:

Gaurav.
 
Jonatan Samoocha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

when skimming through HFS I ran into the same confusion about the "." operator. The mock exam answer on page 432 does clearly contradict with the last "Gotcha" on page 447 (see initial post in this thread).

I'm affraid Kathy's reply in this thread was incorrect, which increased the confusion. After running a test similar to the one used by Gaurav, it seems like trying to access a non-existing bean property does cause an exception to be thrown. This behavior is confirmed by the JSP2.0 spec (see JSP2.3.4, page 1-68 /1-69), which states that an error is thrown if EXPR2 in EXPR1.EXPR2 is not "...a readable property of EXPR1, as per the JavaBeans specification".

Based on the test and the JSP spec, I think we can conclude that (i) the mock exam answer on page 432 is correct, and (ii) the last "Gotcha" remark on page 447 is incorrect.

Please cross-check these conclusions since I don't consider myself a JSP guru (in fact, I'm just beginning to prepare for the exam).
 
Giju George
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even I got a javax.servlet.ServletException
 
Krzysiek Hycnar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Come on you guys, this is simple!!

For the the EL like <c ut value="${bean.propertyThatDoesNotExist}" />

The rule is: if the bean EXISTS, but the property you're trying to access DOES NOT EXIST, you get an exception - in Tomcat it is org.apache.jasper.JasperException with the description kind of - Unable to find a value for "propertyThatDoesNotExist" in object of class "test.scwcd.bean.MyBean" using operator "." (null) (The same will happen if the property exists, but there's no getter.)

On the other hand if the bean DOES NOT EXIST you will NOT get an exception - the code will not print anything.

Cheers
Chris
 
Sameer Raza Ali
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There needs to be a correction in the book

In q 17 The option C is unchecked
If the . (dot) operator is used to access a bean property but the
property doesn’t exist, then a runtime exception is thrown.

should be checked It is true.

This is also tested in question
Q 10- Which show valid usage of EL implicit variables?

option C. ${pageContext.foo}

Option C is incorrect because pageContext is NOT a Map and it doesn’t have a “foo” property.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sameer, that thread is 5 years old. The error has already been added to the errata since then (search for "Page 437" on that page). In future create a new topic for your question instead of replying to a 5 year old thred...
 
Sameer Raza Ali
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry didn't see the date on the last message, but i replied here because this thread came up in google search the next person to visit this thread will have no confusion regarding this...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic