• Post Reply Bookmark Topic Watch Topic
  • New Topic

Catching NullPointerException  RSS feed

 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it necessary to catch a NullPointerException?

I have tested my Applet and it continues to function even though it throws a NullPointerException if there is no String available in the JComboBox (variable: se2). I have handled the Exception in the last lines of my method and I no longer have the NullPointerException error message in NetBeans.

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A proper program should let no exceptions "leak to the top" while running, so yes, you ought to catch this. But far better than catching it is to change the code to recognize that a particular value might be null and test for null, and only use the value if it's not null. Otherwise supply a default, or merely skip the offending code.
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I will see to it that the code is changed to reflect your idea.
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it comes to NullPointerException I check for nulls throughout my code and/or on any public methods arguments, and also rethrow as Invalid Argument exception or some other form of recovery/status message.

Reason being that when debugging, NullPointerExceptions are often the most vague exception you can get. They are among the most common error, and tell you very little about the root cause of the problem without stepping through a lot of code. Its better for debugging purposes that when an error is found, a more descriptive message is reported.
[ December 01, 2005: Message edited by: Scott Selikoff ]
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the additional information Scott
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!