• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception doubt

 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me understand this code



this code is giving null pointer exception
but in the if statement if i put
--> this is giving output string is empty

Please explain me why ??
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the question comes down to the difference between | and ||.

Have you heard the term "short-circuiting operator"? That's the difference. If you are evaluating a || b, as soon as a is determined to be true then we know the entire expression is true. So a short-circuiting operator like || doesn't bother evaluating the second part. But | doesn't short-circuit, so it does still go on to evaluate the second part. And in this case the second part throws an exception.

| means something different when working with numbers, but this is what it means when using booleans. And it's very seldom a good idea to use it like this.
 
Aki Mohan
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to what Matthew said, if you wanna check that out then use || and it will compile fine.
 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why will the second part throws exception??
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aki Mohan wrote:Adding to what Matthew said, if you wanna check that out then use || and it will compile fine.

That's wrong! Both the version with | and || will compile fine.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meghana chintanippu wrote:why will the second part throws exception??

It's explained perfectly by Matthew Brown. If you use | then both parts are always evaluated. When you use || (a short-circuit operator), only the left part (str==null) is evaluated. You can read the reason why in Matthew's post.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meghana chintanippu wrote:why will the second part throws exception??


The second part throws an exception, if it is evaluated, because it's being called with str as null. You can't get the length of a null string, because you can't call a method on a null value. So the difference is whether the second part is evaluated or not.
 
meghana chintanippu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much for the explanation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic