• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Why NullPointerException is defined as RuntimeException?

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.


Why NullPointerException is defined as RuntimeException?
Most of the time NullPointerException is the result of a programming error and it takes a hell lot of logging,debugging and time to get rid off of it.
wasted full morning debugging the faulty code of a careless programmer
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why NullPointerException is defined as RuntimeException?


Otherwise every access to a method or member would have to be enclosed within a try/catch block, yuck
And that of course won't prevent them from happening, just force you to deal with them explicitly.
Although in my experience, NPE's are usually easy to track down as long as you do a printStackTrace() and the offending line number (or at least the method) is available.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why NullPointerException is defined as RuntimeException?
Most of the time NullPointerException is the result of a programming error

Um, most RuntimeExceptions represent programming errors, in my experience. I often refer to them as SPE's - Stupid Programmer Errors. Checked exceptions, on the other hand, are more often things that weren't really under the programmer's control. These are generalizations, certainly not always true - but still, true more often than not.
it takes a hell lot of logging,debugging and time to get rid off of it.
It shouldn't. I agree with Blake. Stack traces are your friends. If you catch an exception and you're not sure exactly why that exception might have been thrown, print a stack trace. It's really easy. I see code all the time where people do something silly like

No! This hides the stack trace, and is a huge waste of time. Instead:

Or if you've set up logging (which you really should) then you have access to other methods which will record the stack trace. Use them! There's no reason why a NullPointerException should be hard to track down.
 
achit bhatnager
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you Jim/Blake. Tracing null pointer should not be a big deal when coding is done with some discipline
I think my earlier post was more of a reaction then reason.
I joined this project mid way. The last guy was planning his exit for couple of months and did not do a good job. There are couple of new (less then 1 year exp on java) guys. He was suppose to do code reviews and in general mentor these new guys.
He did nothing...in the disguise of using XP methodology he asked these junior programmers to start coding with no design specs and forgot to tell about unit testing/refactoring/code reviews... .now the source is full of bs code ..almost no error checking, no logging and specially no stack trace.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
achit,
sounds suspiciously just like life to me Sometimes it will be a hassle to sort out poor code others have written, but this is part of what we do I guess. At least it keeps us in work
I agree, for an experienced programmer, null pointers are fairly easy to track down most of the time.
I'm not sure how null pointer could be picked up at compile time anyway. How can the compiler know what interaction and object creation will occur within a program before it is running?
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only place where I have found it difficult to track the NPEs are when the code looks something like this

You never know, which object is null here, even with stack traces. You will need some debug code to find out what went wrong.
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Mani Ram:
The only place where I have found it difficult to track the NPEs are when the code looks something like this

You never know, which object is null here, even with stack traces. You will need some debug code to find out what went wrong.


AMEN! I'd love to strangle a couple contractors at my job for leaving code like this.
    Bookmark Topic Watch Topic
  • New Topic