• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EL is null Friendly?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HFSJ on page (395) it is written EL handles null values gracefully.
Even if foo attribute is not present ... ${foo} will print nothing(blank) & will NOT throw NullPointerException.

But in practice exam Page-424

Q17 Which about EL access operators are true.

A. Anywhere the .(dot) operator is used, the [] could be used instead.
B. Anywhere the [] operator is used, the .(dot) could be used instead.
C. if .(dot) operator is used to access a bean property but the property doesn't exist, then a runtime exception is thrown
D. There are some situations where the .(dot operator must be used and other situations where the [] operator must be used.

Answer given is A & C
I am not convinced with answer C.

Any suggestions or clarification?

Thanks
Gagan.

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is if I have a bean named person of type Person and the person class looks like this
Now if the name of the person bean is null then using ${person.name} will result in a blank value, but ${person.gender} will result in an exception. EL only gives blank if either there is no bean named person in any scope or if the property you access on the person bean exists and is null. If you access a property on a bean which doesn't exists (like gender in our example) then an exception is raised...
 
Deep Gagan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit

Thanks for the information. But i am not convinced, as the followig text is given in the HFSJ(page-395) matches our discussion.

<snippet from book>
Assume that there is not an attribute named "foo", but there IS an attribute named "bar", but that "bar" does not have a property or key named foo.
${foo}
${foo[bar]}
${bar[foo]}
${foo.bar}

According to book all of these EL will print blank.

I will try this & share the info with you.

Thanks
Gagan.
 
Deep Gagan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit

I have tested these scenarios... & your following lines are absolutely correct....

EL only gives blank if either there is no bean named person in any scope or if the property you access on the person bean exists and is null.
If you access a property on a bean which doesn't exists (like gender in our example) then an exception is raised...


Also can you please get this to writer's notice that this info is wrong in the book, so that they can correct in the next version.

Thanks
Gagan.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deep Gagan wrote:Also can you please get this to writer's notice that this info is wrong in the book, so that they can correct in the next version.


If you have erratas to submit, submit them at the publisher's homepage.
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deep,

It is not an error in the book. In El there a two ways of accessing a property of a bean:

That is however different from ${bar[foo]}. This construction is used for Arrays Lists and Maps.

In this case everything inside the brackets is evaluated first and because foo is null the ${bar[null]} will also be null
have a look at

JSP.2.3.4
expr-a.identifier-b is equivalent to expr-a["identifier-b"]; that is, the identifier
identifier-b is used to construct a literal whose value is the identifier, and then the []
operator is used with that value.

To evaluate expr-a[expr-b]:

  • Evaluate expr-a into value-a
  • If value-a is null, return null.
  • Evaluate expr-b into value-b
  • If value-b is null, return null.



  • Regards,
    Frits

     
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Frits Walraven wrote:That is however different from ${bar[foo]}.


    It is no different.

    ${bar[foo]} can be used for beans as well as lists or maps as long as foo evaluates to a valid bean property name.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3411
    320
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    ${bar[foo]} can be used for beans as well



    You are absolutely correct, I didn't mention that part of the specs.

    JSP.2.3.4
    .....[removed]
    Otherwise (a JavaBeans object), coerce value-b to String
    -If value-b is a readable property of value-a, as per the JavaBeans specification:

  • If getter throws an exception: error
  • Otherwise: return result of getter call

  • -Otherwise: error.


    This will only be the case is if foo evaluates into a property-name. However in this question "foo" evaluates to null and therefore ${bar[foo]} returns an empty String.

    Conclusion: no mistake in the the book.

    Regards,
    Frits
     
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Conclusion: no mistake in the the book.



    That means bar["foo"] will throw an exception but NOT bar[foo] is this particular case ?

    Regards,
    Nitin
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3411
    320
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Nitin,

    bar["foo"] will throw an exception but NOT bar[foo]


    You are correct!

    bar["foo"] will throw an error as foo is not a property of bar
    bar[foo] will evaluate into an empty String as foo is not defined as an attribute in any of the scopes

    Have a look at my notes on the Scwcd links section (for a few more examples)

    Regards,
    Frits
     
    Nitin Kumar
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Frits,

    Thanks for the confirmation and yes your notes are really helpful.

    Regards,
    Nitin
     
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In the new Edition 2.0 The answer is only A.

    Cheer,
    Niraj
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3411
    320
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    In the new Edition 2.0 The answer is only A.


    and that is a mistake in the book, just check the specifications on this matter (JSP v2.0, section JSP.2.3.4):

    Regards,
    Frits
     
    Niiraj Patel
    Ranch Hand
    Posts: 47
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks brother..!!
     
    reply
      Bookmark Topic Watch Topic
    • New Topic