4

"Why did my program crash with Swift fatal error: unexpectedly found nil while unwrapping an Optional value?" is one of the most frequently asked questions in the [Swift] tag.

In many cases the question can be closed as a duplicate to one of the many existing questions, including at least one that has some detailed troubleshooting steps in the answers.

In some cases though, it is merely that the wrong question has been asked. I came across two questions on this this morning.

The first I initially closed as a duplicate, but after reading the documentation for the framework involved for about a minute I was able to see what the asker was doing wrong, re-open their question and give an answer that solved their problem.

In the second case, the question had been closed as a dupe by another gold badge, but the underlying problem (a malformed URL) was easy to see. In this case I again re-opened and provided an answer.

While I understand all too well the frustration of seeing the same question again and again, it seems that by reflexively closing questions as duplicates as soon as we see "unexpectedly found nil" we are preventing (or at least making it more difficult) to for people to get the help they came here for.

Edit

@Keiwan said in a comment

I definitely agree that nobody should vote to close anything without actually reading it first (no matter how triggering the title is), but I also think that pretty much everybody would agree on this

But it seems this is not the case, the reflex triggering based on question title is happening.

I guess I am asking what, if anything, we can do about this? Perhaps nothing? Just reopen or flag for reopen? Look for a better dupe-target (one that addresses the root cause, not the error message)?

I am sure that there are similar classes of questions in other tags as well. This is the Swift equivalent of "null pointer exception" in C and can have numerous root causes; some of which are obvious duplicates, some of which are not.

Does unnecessary dupe-hammering happen with those or is this a Swift tag issue?

7
  • While trying to find a question in this post my brain unexpectedly found nil and crashed. Jokes aside I definitely agree that nobody should vote to close anything without actually reading it first (no matter how triggering the title is), but I also think that pretty much everybody would agree on this. So again, I'm not sure what a potential answer to this question could be (or any discussion really). Maybe you could clarify that a little bit.
    – Keiwan
    Commented Mar 22, 2017 at 22:36
  • This does happen in other tags as well (Java, C#,...) but in probably over 99% of the cases the question is actually correctly closed as a duplicate. If someone wrongly closes it because they didn't actually read the question I assume that they themselves would agree that they should have read it in the first place (which is what I was trying to say with my comment). As for the specific question you've linked to I wouldn't say it's very clear (as in you might have also just left it closed as a duplicate). The OP didn't ask a clear question which you've noted in your answer as well, but I also
    – Keiwan
    Commented Mar 22, 2017 at 22:53
  • see that they were really trying to ask why the URL initializer returns nil even though they are providing a seemingly valid string. The problem is that they didn't actually ask that so I think you can't for sure say that either you or the people trying to close the question are wrong.
    – Keiwan
    Commented Mar 22, 2017 at 22:55
  • Related, somewhat: meta.stackoverflow.com/questions/337254/…
    – jscs
    Commented Mar 22, 2017 at 22:59
  • 1
    This type of closing isn't specific to Swift, consider Java and the various NPE duplicates.
    – JAL
    Commented Mar 22, 2017 at 22:59
  • 2
    And I humbly suggest that my answer (not the question): meta.stackoverflow.com/a/261574 is relevant. If you see important ways the question is different, please edit, especially the title.
    – jscs
    Commented Mar 22, 2017 at 23:04
  • @JoshCaswell Good point. I have edited one of the questions to more correctly describe the problem.
    – Paulw11
    Commented Mar 22, 2017 at 23:11

1 Answer 1

7

I would generally say yes, the majority of posts about unwrapping optionals should be closed as a dupe of the Community Wiki Q&A pair. It's meant to be a comprehensive source of all of the possible ways the error can occur. And the majority of questions that ask about this issue are from users without a fundamental understanding of how Optionals in the Swift language works.

Let's take a look at your second example, since that's the one that I originally hammered and you reopened.

The "question"

swft fatal error: unexpectedly found nil while unwrapping an Optional value

hello I get an error don't know why because it shows value but says nil

To me, this shows a lack of understand of Optionals, and thus should be directed to the canonical.

Why are they getting the error? Because they're force unwrapping an optional.

If we want to take it one step further, we could explain where they're seeing the optional. The question should probably be rewritten to ask:

  1. Why am I getting this error?

  2. Why is the URL constructor returning nil?

This is a perfect use of the new multiple duplicates feature. I would also add this question or this one as a duplicate, so the user can understand why the URL constructor is returning nil.

5
  • Interestingly Fogmeister's answer (while arguably correct and basically the same as I provided) doesn't actually answer the question that was asked in that case either. The question was "how do I get the failure reason. The answer describes common causes for failure and how to avoid it. My concern over that as a duplicate target is that the question is substantially different and the answer is not Swift 3, which is likely to confuse a newcomer to Swift.
    – Paulw11
    Commented Mar 22, 2017 at 23:19
  • While the question does show a lack of understanding about optionals, there was also sufficient information in the question (code and the URL string in question) to diagnose the root cause, so it wasn't the typical terrible "unwrapping nil" question.
    – Paulw11
    Commented Mar 22, 2017 at 23:19
  • 1
    How about this one then? Either way, these are issues that can easily be found by searching on Stack. I don't really see the value in keeping these questions around. If both of the questions in the post have been asked before, and we can find valid duplicates, why not close it as a dupe?
    – JAL
    Commented Mar 22, 2017 at 23:20
  • Yep. Thats a good one. I have closed as a duplicate of that. I guess that was my point in starting this discussion. Perhaps we should spend time looking for better dupes than simply closing as "go learn what an optional is" (as useful as that may be in some cases)
    – Paulw11
    Commented Mar 22, 2017 at 23:20
  • @Paulw11 You're absolutely right, and I'm excited to use the new multiple dupe feature to do so. Thanks for taking this to meta, so we can discuss this openly!
    – JAL
    Commented Mar 22, 2017 at 23:25

You must log in to answer this question.

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