• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

shrink a few lines of repeated code.

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is one method from Hashset Iterator. it returns the next object from the current one. I have a NullPointerException while running "if(current==null|current.next==null)" so I added the try and catch and it works. First question is, why am i getting the nullpointer exception if i have previously checked hasNext(). 2nd question, since the catch{} has the same code in the try{}, is it possilbe to strink some of the catch{}? Many Thanks.
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What line in the above code caused the NPE?

WP
 
Willie Tsang
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
eclipse said "if(current==null|current.next==null)"
 
Marshal
Posts: 28291
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha! Change this line of code from

to


Why? The "|" operator always evaluates both of its operands: so even if current == null is true, it will still look at current.next == true, thus throwing the NPE for current.next. Whereas the "||" operator will only evaluate its RHS if its LHS is false. The latter is what you want in this case, and frankly in almost every case.

And don't ever respond to NPE by writing code which catches it. A NullPointerException always indicates a programming error which should be found and fixed, rather than ignored and worked around.

(Of course when I say "don't ever" and "always" there, there are always exceptions to such things. But if it's your code, that isn't one of the exceptions.)
 
Willie Tsang
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot, that was a clear explainion. and thanks for the advice.
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic