-17

I believe that this question has value on the site and should be undeleted.

Unlike the duplicate target that asks about "what is" an NPE and "how to fix" it, this question asks for all an exhaustive list of all the possible ways to get an NPE. I think it is a question that many people who gets frustrated over NPEs would be wondering about, as can be seen by the 4 upvotes on the question.

On the one hand, if you combine all the duplicate target's answers, you can still get a list of possible ways to get an NPE. On the other hand however, you don't get an argument for the exhaustiveness of the list. You also have to look through all 12 undeleted answers there, which takes a long time.

If this question is undeleted, one can simply go to that question to find an answer (note: my answer) that has everything compiled instead. In my answer I also mentioned using method references on a null reference, which apparently no answer in the duplicate target did.

According to this, the post should not have been deleted because it serves as a good signpost to the duplicate. One may encounter an NPE because of the same reason as the OP, and wonders what other ways an NPE could occur. In this case this question would be very useful, because not only does the person sees a comprehensive list of situations in which an NPE occurs, they can also go to the duplicate target to get information about how to fix it.

I am also open to the idea of posting my answer, which shows a rather exhaustive list, to the duplicate target, but I'm not sure if it actually answers the "how to fix it" part of the duplicate question, and also because the duplicate question is locked and I can't post new answers :(

(Note that I'm not also saying the question should not be a duplicate as well. I think it would be a great pointer to the duplicate target.)

Screenshot for the under 10k:

Enter image description here

6
  • 1
    What are you looking for in an "exhaustive list"? To use C terminology: any attempt to dereference a null pointer will result in an access violation. The only other way is to manually instantiate a new NullPointerException and throw it. That's the list. None of these cases are surprising if you understand what unboxing is (contrary to the fancy name, it's just performing a basic function call), how string concatenation works (more function calls), and why upcasting is a no-op.
    – vandench
    Sep 7, 2022 at 2:37
  • @vandench The thing is, there are no pointers, or dereferencing, in the context of Java, so “any attempt to dereference a null pointer” doesn’t really work as an answer in my opinion.
    – Sweeper
    Sep 7, 2022 at 3:01
  • What is the canonical meta question (or blog post) about list of X questions? Sep 7, 2022 at 9:14
  • If it isn't undeleted, then I suggest to just edit the top answer of the dupe with the new information from your answer. Because that's essentially what canoncials are for (or, how they are used today). All the information compiled into a single Q&A-Pair.
    – Lino
    Sep 7, 2022 at 9:51
  • @Sweeper But there are pointers, that's why it's called a Null Pointer Exception. Java just tries to hide that reality. The accepted answer on the dupe target even uses the same terminology of 'dereferencing'. I can't think of any other information that could be elicited that isn't already in the canonical.
    – vandench
    Sep 7, 2022 at 13:38
  • 2
    @Lino Seeing the votes on this meta post, I have just gone there and did that.
    – Sweeper
    Sep 8, 2022 at 1:09

2 Answers 2

9

Unlike the duplicate target that asks about "what is" an NPE and "how to fix" it, this question asks for all an exhaustive list of all the possible ways to get an NPE.

Quoting directly from the duplicate target:

What are Null Pointer Exceptions (java.lang.NullPointerException) and what causes them?

Even if it isn't expecting one specific answer to have a comprehensive list, answers that show causes are on-topic, and answers that show causes not already mentioned are not duplicates.

That said, I don't think this example meaningfully constitutes a novel way of getting NullPointerException. There is already an answer that states:

In fact, the only things that you can do with a null without causing an NPE are:

  • assign it to a reference variable or read it from a reference variable,
  • assign it to an array element or read it from an array element (provided that array reference itself is non-null!),
  • pass it as a parameter or return it as a result, or
  • test it using the == or != operators, or instanceof.

Unboxing - i.e, type converting from Long to long - is not on that list.

Although if you combine all the duplicate target's answers, you can still get a list of possible ways to get an NPE, but you don't get an argument for the exhaustiveness of the list. You also have to look through all 12 undeleted answers there, which takes a long time.

It is not realistic to ask for an exhaustive list of distinct ways to cause an error, anyway, because it is not realistic to have objective criteria for whether two proposed ways are distinct. As we've already seen, it's far simpler and more straightforward to list things that don't cause NPE with a null value.

But aside from that - why is it useful to have a list like that to refer to, if you already have a general understanding? Will it help with debugging, for example by giving you a list of specific things the code might be doing that causes the NPE? No, because you get a stack trace, so you know exactly where in the code the problem is, and you know what that code is trying to do; so the next step is to determine what's null that shouldn't be; and then (depending on the code requirements) either a) adapt the code to allow for that possibility; b) work backwards to figure out why it's null, and make sure that doesn't happen.

I am also open to the idea of posting my answer, which shows a rather exhaustive list, to the dupe target, but I'm not sure if it actually answers the "how to fix it" part of the dupe question, and also because the dupe question is locked and I can't post new answers :(

It's locked for a reason: people kept trying to add useless, redundant answers. If you think an existing answer can be improved, propose an edit. But there is only so much that can be usefully said about one error message.

2
  • "why is it useful" Because it would serve as a good signpost to the duplicate. I'd imagine many people who knew about NPEs, like the OP, may not expect them to occur when unboxing a null value, and would want an exhaustive list. They would then find this question, and if they need debugging help they would go to the dupe target. Also, when I answered it, it had 1 or 2 upvotes, so clearly some people see value in this question. The score then went up to 4 at some point even after being closed, and it is only when I noticed that my answer was deleted that I saw it dropped to a score of 1.
    – Sweeper
    Sep 7, 2022 at 3:45
  • @Sweeper A couple of votes in a site that is visited by millions... not exactly representative. Pretty much negligible to be honest, especially because there are plenty of people that vote to be nice.
    – Gimby
    Sep 7, 2022 at 10:50
5

This Q&A pair is distinct from the duplicate target because it's not about debugging an exception that's already been thrown. It's about understanding what could possibly throw a NullPointerException.

For example, imagine you're trying to assess a piece of critical code for null-safety. You need to know, for certain, that it won't throw an NPE no matter what you throw at it, including via inspection of the generated bytecode if need be.

In such a case, the answer exhaustively listing ways an NPE can be thrown would be useful. You could examine each possible case where a possibly-null value is handled, and ensure that it doesn't do any of the things that throw the exception. Of course, you should also verify this with tests, but that doesn't mean the tests can't be inspired by an inspection of the code.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .