wood burning stoves*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes EL is null Friendly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "EL is null Friendly?" Watch "EL is null Friendly?" New topic
Author

EL is null Friendly?

Deep Gagan
Greenhorn

Joined: Aug 03, 2007
Posts: 11
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.


Thanks & Regards
SCJP, SCWCD
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deep Gagan
Greenhorn

Joined: Aug 03, 2007
Posts: 11
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

Joined: Aug 03, 2007
Posts: 11
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.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1686
    
  25

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

    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61310
        
      66

    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.

    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender

    Joined: Apr 07, 2010
    Posts: 1686
        
      25

    ${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
    Nitin Kumar
    Greenhorn

    Joined: Apr 18, 2005
    Posts: 29
    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
    Bartender

    Joined: Apr 07, 2010
    Posts: 1686
        
      25

    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

    Joined: Apr 18, 2005
    Posts: 29
    Hi Frits,

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

    Regards,
    Nitin
    Niiraj Patel
    Ranch Hand

    Joined: Sep 17, 2011
    Posts: 47
    In the new Edition 2.0 The answer is only A.

    Cheer,
    Niraj
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender

    Joined: Apr 07, 2010
    Posts: 1686
        
      25

    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

    Joined: Sep 17, 2011
    Posts: 47
    Thanks brother..!!
     
    GeeCON Prague 2014
     
    subject: EL is null Friendly?