• 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

Why the code throws Exception?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of these 'if' will throw NullPointerException?
String s = "hello";
s = null;
if((s != null) && (s.length() > 0))
if((s != null) & (s.length() > 0))
if((s != null) | | (s.length() > 0))
if((s != null) | (s.length() > 0))
If have any explanations on this topic, I'd appretiate this.
Thank a lot.
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aull,
In answer to your question, the first if statement is the only one which will NOT throw a NullPointerException.

The reason has to do with what expressions are evaluated with certain operators.
With the && operator, if the first expression is false, the second expression is never evaluated. That is why the first if statement does not throw an exception.
With the & operator, both expressions are evaluated. Since s IS null, we get the NullPointerException when we try to evaluate s.length();
With the | | operator, if one operand is true, the result is true without regard to the other operand. In the case of the third if statement, however, the first operand is false, so the second operand is evaluated and throws an exception.
Hope this helps.
Stephanie
 
Aull Sahar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stephanie Grasson:
[B]Aull,
In answer to your question, the first if statement is the only one which will NOT throw a NullPointerException.

The reason has to do with what expressions are evaluated with certain operators.
With the && operator, if the first expression is false, the second expression is never evaluated. That is why the first if statement does not throw an exception.
With the & operator, both expressions are evaluated. Since s IS null, we get the NullPointerException when we try to evaluate s.length();
With the | | operator, if one operand is true, the result is true without regard to the other operand. In the case of the third if statement, however, the first operand is false, so the second operand is evaluated and throws an exception.
Hope this helps.
Stephanie[/B][/Q
Thank you very much, You've opened my eyes

reply
    Bookmark Topic Watch Topic
  • New Topic