-3

Often times when writing programs, I come across a NullPointerException that I didn't expect. I often spend forever finding what exactly is null, only to find out that it was a simple oversight in my part.

What I want to know is: why NullPointerException isn't generic.

for example:

    java.lang.NullPointerException<(Class of object that is null)>
        at (Stack trace)

instead of:

    java.lang.NullPointerException
        at (Stack trace)

To me, this would considerably reduce the time spent debugging and seems like it wouldn't be hard to implement.

Incidentally, how exactly do catch blocks work with generic exceptions anyway?

Sebastian
  • 108
  • 1
  • 9
  • 5
    `"I often spend forever finding what exactly is null"` -- Why is that? The NPE stacktrace tells you exactly which line is throwing the exception, and it's usually quite easy to find out which object is null. How would your suggestion help at all? – Hovercraft Full Of Eels Jun 24 '14 at 21:16
  • 2
    Usually, you can find the `null` value in the first line of the *at (Stacktrace)* part. – Luiggi Mendoza Jun 24 '14 at 21:16
  • One line of code can contain references to many objects. Knowing which line doesn't always tell you which object. – Sebastian Jun 24 '14 at 21:19
  • [Exceptions can't be generic](http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html) because of type erased at runtime. – Pshemo Jun 24 '14 at 21:19
  • NPE is one of the easiest exceptions to debug. Besides, what specific data are you going to put into the exception? You don't know the class of the object since you don't have its pointer. – Hot Licks Jun 24 '14 at 21:19
  • I guess it's because nobody needed it. As said, you can easily read where the problem is from the stack trace (if you have source code to your code). – kajacx Jun 24 '14 at 21:20
  • 4
    `"One line of code can contain references to many objects. Knowing which line doesn't always tell you which object."` -- That's a strong argument for not trying to compress your code too much, to instead strive to write clean debuggable code. – Hovercraft Full Of Eels Jun 24 '14 at 21:20
  • 1
    The NPE stacktrace tells you exactly which line is throwing the exception. In most cases this is enough to find out which object was null. If it's not, consider refactoring your code, as you doing too much on a single line of code. – dant3 Jun 24 '14 at 21:20
  • @Pshemo - Well, you probably could define a generic exception type, but it would be pretty silly to do so. – Hot Licks Jun 24 '14 at 21:22
  • If don't know where on your line you have NPE, try spliting your line into more. If not possible (e.g. many method parameters), test your values for `null` individually. It's called debugging. – kajacx Jun 24 '14 at 21:22
  • 1
    @HotLicks Throwables -- including Exceptions -- cannot be generic ([JLS, Section 11.1.1](http://docs.oracle.com/javase/specs/jls/se8/html/jls-11.html#jls-11.1.1)). – rgettman Jun 24 '14 at 21:26
  • Possible duplicate: [Why doesn't Java allow generic subclasses of Throwable?](http://stackoverflow.com/questions/501277/why-doesnt-java-allow-generic-subclasses-of-throwable) – Pshemo Jun 24 '14 at 21:28
  • @rgettman - Ah, one of the few places where generics were disallowed simply because they didn't make sense. (And, of course, one could add the class of the null pointer as a value stored in the NPE or a subclass of it -- if it made sense, which it doesn't. Generics are not needed (and even more useless here than in other areas of the language).) – Hot Licks Jun 24 '14 at 21:35

4 Answers4

2

From javadocs: Generics as an enhancement allows a type or method to operate on objects of various types while providing compile-time type safety. But there is no way you can know at compile time whether an object is null or not.

The null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type

Vivin
  • 1,327
  • 2
  • 9
  • 28
  • 1
    `List` and `ArrayList` are way before generics but these classes were updated to support generics. This is not a reason. Also, how can you define which type is `null`? – Luiggi Mendoza Jun 24 '14 at 21:20
  • The null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type - Javadocs – Vivin Jun 24 '14 at 21:23
  • Then there would be no point to support generics for `NullPointerException`. Still, you don't answer the first sentence of my previous comment. – Luiggi Mendoza Jun 24 '14 at 21:24
  • I edited my answer. What do you say? – Vivin Jun 24 '14 at 21:27
  • 1
    A bit better but looks like you don't understand my point. *Because generics came way after NullPointerException was created.* this sentence is misleading because collections existed since Java 1.0 (like `Vector`) and they support generics that came on Java 5. – Luiggi Mendoza Jun 24 '14 at 21:28
  • I understand your point. Let me remove that. Thanks – Vivin Jun 24 '14 at 21:30
2

The debugger is designed to reduce time debugging.

A null has no type so you can't add a type to an NPE.

If you have many expressions which could be null on the same line, you are trying to do too much one one line (or you should use a debugger)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

The point is that a variable is a pointer to a class that is not defined until runtime (normally you define a variable with an interface and instantiate with an implementation). So, if you receive a null pointer exception it means that... this pointer has not been initialized so... it is not possible to know which class is it (because it points to nothing).

Carlos Verdes
  • 3,037
  • 22
  • 20
  • Yes, but since Java is type safe, it should know which class it is *supposed* to be, or at least a superclass thereof – Sebastian Jun 24 '14 at 21:23
  • 2
    @Sebastian in that case you would have to ensure all your references use a different type. Why not just use a debugger? – Peter Lawrey Jun 24 '14 at 21:27
0

NPE appears when your variable is null. =>So your variable isn't of any type.

The purpose of NPE is to tell you "hey man, you're trying to do something on nothing". Not to tell you why there is nothing.

It would be too much time consumming to tell why there is an error.

naiimco
  • 75
  • 1
  • 11