38

At least once a week, a user will ask a question on one of the sites as to why their model, or a property of their model is null when they submit their form back to the controller's action method.

The majority of these questions are also tagged and often these questions are closed as a duplicate of (the excellent) What is a NullReferenceException and how do I fix it?, usually by a well meaning gold badge owner who is, I assume, not familiar with MVC, and in particular the model binding process.

There are at least half a dozen reasons why the model could be null, including this one I answered yesterday. Others include using foreach loops instead of for loops to generate collections in the view, using fields instead of properties in the model, including hidden inputs for properties which are complex objects, etc. Unless the OP was to download and step through the source code to understand how the DefaultModelBinder code matches up a form's name/value pairs to model properties, the answers in the duplicate above are not going to help solve the problem.

Prior to my earning a gold badge in , all I could do was leave a comment and a link to another answer which did explain and solve the problem. Now that I have it, I've been re-opening the questions, but as soon as I try to close it again as a duplicate of a question that does answer it, I'm told I have to wait a couple of weeks. In the meantime, the question gets closed again as a duplicate of What is a NullReferenceException and how do I fix it?, so I'm back to leaving a comment, which might help the OP, but perhaps not others, especially if its lost in a forest of other comments.

I had thought of editing the accepted top answer, but its already long enough, or adding my own answer to address specific issues but that will just rot at the bottom - I got a blister scrolling to the bottom of the page as it is :)

Are there any other options I have to ensure the duplicate is the correct one to answer the question.

7
  • 5
    "What is a NullReferenceException and how do I fix it?" and the same for Java, ... is just a shortcut and it's in general useful for handling a lot of similar questions. But in general it is too broad and that has advantages and disadvantages. The disadvantage is that you have to read large answers or a lot of answers to really get your solution. You could nevertheless post your answer and bet on the meta effect. A 100 votes are easily obtained here on meta if it is necessary. Commented Jan 20, 2016 at 8:46
  • 2
    "... but as soon as I try to close it again as a duplicate of a question that does answer it, I'm told I have to wait a couple of weeks." That's actually the part I do not understand. Why is it not closed as a duplicate of the question that really answers it and who is saying that you have to wait a couple of weeks and what are you supposed to wait for? Commented Jan 20, 2016 at 8:48
  • 7
    Yeah, model binding is magic and can't be treated like normal NRE questions. Debugging NREs normally don't involve chicken entrails.
    – user1228
    Commented Jan 20, 2016 at 14:07
  • @Trilarion, If I use my dupehammer to reopen a question, the system simply wont let me re-close it again as a dupe of another answer - I get a popup saying I have to wait.
    – user3559349
    Commented Jan 20, 2016 at 23:41
  • @StephenMuecke I see. Looks like the system could be improved then. Commented Jan 21, 2016 at 9:44
  • 3
    I've often come across cases when question is incorrectly flagged as duplicate Commented Jan 21, 2016 at 19:43
  • 1
    I saw a question--I forget what now--that was closed as a duplicate and the marked duplicate literally had nothing to do with the question asked. Best analogy I can come up with is closing a "why is this null?" question marking it as a duplicate of "what is regex?" The question was legitimately a duplicate one, just not a duplicate of the one it was closed for. Commented Jan 21, 2016 at 20:04

3 Answers 3

8

This is in regards to

Now that I have it, I've been re-opening the questions, but as soon as I try to close it again as a duplicate of a question that does answer it, I'm told I have to wait a couple of weeks.

I have never seen this warning and I have personally reopened a question to close it as a dupe of another one with my dupe hammer. If we look at the history of String not getting stored in arr[0], while using getline(cin, arr[i]) we see:

enter image description here

You will notice that both actions happened in the same minute and if you hover over the time a tool tip will come up and you can see there was a 6 second difference between the actions. As a courtesy to the original close voter I did leave a comment explaining my change of the dupe target.

You should be able to do the same thing. The only thing I can think of that might be causing an issue is that it was closed with one gold tag and reopened with another. I don't know why that would matter but maybe it does.

EDIT:

I just had a question that I reopened for an incorrect dupe and closed it as a dupe of another program so AFAIK this behavior still has not changed. If I can find some people to run a test with I am going to try and see if different tag badges makes any difference.

4
  • CodeCaster made a similar comment in his answer so I'm not sure what's going on (and I now wish I had taken a screen shot). I'm about to have a weeks holiday, but when I get back I'll search for something to try it out on again.
    – user3559349
    Commented Jan 22, 2016 at 2:03
  • "closed with one gold tag and reopened with another" - I would like to help testing this but I have to answer a few more questions before. :) Anway, this should be easy to check for two gold tag members. Commented Jan 22, 2016 at 12:56
  • @Trilarion Yes we could test it. Just need to get a couple of gold badgers together and find a good question to change. Commented Jan 22, 2016 at 12:59
  • This is how it's supposed to work. Let's hope there is no new "feature" that lets us wait a few weeks - if there is, it should be reverted asap.
    – Bergi
    Commented Jan 22, 2016 at 16:02
5

It depends on how the question is asked.

If the question really is "Why do I get a NullReferenceException at model.Foo", then it's perfectly fine to close as a duplicate of the canonical "What is a NullReferenceException and how do I fix it?", because the phrasing alone shows that OP lacks basic debugging and searching skills.

See also "Null Pointer Exception" questions closed as duplicates.

Feel free to create a more specific canonical MVC Q&A about how the model binder works and how to properly generate a form that will be successfully bound, but until that exists, I'm fine with the closing you see.

And in the end, it always comes down to "You messed up your form generation code", so I think it'll be fairly easy to create a canonical Q&A about this.

You really don't want to spend your effort to explain every variation of these typos and misunderstandings, while as you say yourself, all those errors fall in like a handful of categories.

I'm also interested in this (I'm trying to be quite active in the asp.net-mvc tag as well, and I see multiple of these questions on a daily basis), so perhaps we can coordinate creating this Q&A.

9
  • 1
    Unfortunately its not always "You messed up your form generation code" If you look at the answer I linked to, the form was posting the correct values. That one was not closed but there was a similar one that was dupehammered and heavily downvoted (cant find it, so I assume now deleted). Telling OP to "debug your code" is no help at all because the answer is in understanding the 'magic' of the DefaultModelBinder.
    – user3559349
    Commented Jan 20, 2016 at 22:58
  • Creating a new canonical Q/A wont help in the case where a question has already been closed because the system wont let you open and then re-close as a duplicate of another question. And in any case I have tried this before for example this one. Within the first 30 seconds it had 3 downvotes (although 2 were quickly reversed) and even though its ultimately been voted up, its pretty disheartening.
    – user3559349
    Commented Jan 20, 2016 at 22:59
  • I'll see what transpires over the next week, and if there are no better suggestions, I think I will add a community wiki, MVC specific answer that address the common causes why model binding fails in a [HttpPost] method.
    – user3559349
    Commented Jan 20, 2016 at 23:01
  • In a previous question (it was a case of the model having fields, not properties if I remember correctly), I successfully re-opened it with my gold badge, but as soon as I tried to close it as a dupe of another more appropriate question/answer I got a popup saying I had to wait (2 weeks I think) so unless I made some other mistake, it does not appear to be the case.
    – user3559349
    Commented Jan 21, 2016 at 6:42
  • @Stephen I've never seen that, in fact last week I saw a question het dupehammered and reopened like thee times in a couple of hours.
    – CodeCaster
    Commented Jan 21, 2016 at 6:49
  • 1
    But was it opened and then re-closed by the same user? Next time something appropriate comes up, I'll try it again just to be sure.
    – user3559349
    Commented Jan 21, 2016 at 6:54
  • 1
    @CodeCaster A user's reputation does not directly relate to his knowledge about how the SO system works. In this specific case, I did not initially realize that Stephen Muecke created this question to help future readers (more like documentation) & i don't expect such a question from him usually, hence doubted that it could be a bug ( I did not open a new bug-post in meta about it !). I am still a learner and believes that NO SOFTWARE IS BUG FREE and tend to question when i see a strange behavior which i am not familiar with. Peace to all :)
    – Shyju
    Commented Jan 22, 2016 at 18:34
  • 1
    @Shyju alright, I may have worded that somewhat too strongly. Apologies for that.
    – CodeCaster
    Commented Jan 22, 2016 at 18:36
  • NP @CodeCaster. All Good :)
    – Shyju
    Commented Jan 22, 2016 at 20:47
2

Unless it is good question (1+ vote) closing with one duplicate and comment about another should be enough. OP has info and if question ends up with negative score it will be cleaned by Roomba later.


In most cases I've seen similar behavior OP ignores all available debugging guidance and posts question with NRE (which indeed asks for duplicate of "What is NRE and how to fix it") instead of figuring out that whole/partial model passed in as null in controller.

If you feel generous you can edit question to clarify that root cause of NRE is binding (also it may be hard without building whole project), but it is hard to recommend as general approach as it does not scale.

My recommendation: if OP failed to debug root cause of NRE - closing as duplicate of "What is NRE and how to fix it" is perfectly valid and give OP chance to debug code and update post with extra information. At that point question can be re-opened and closed as correct duplicate.

If OP already provided clear information where null comes from - closing as duplicate of "What is NRE" is uncalled for and commenting to close voters may be better approach. Especially if close was done by gold badge holder it should be trivial to ask to re-open.

If question does not look worth fighting for (most of those are about the same - wrong naming) adding comment "Root cause is duplicate of ...." or "You can find more info by searching for {list of keywords}". There is really no huge difference for most of posters if question is duplicate of one or another question.

3
  • 1
    The problem I have with "if OP failed to debug root cause of NRE" is that I think is unreasonable for OP to interpret "debug your code" also means download and debug Microsoft's source code (see my first comment to CodeCaster answer). However "commenting to close voters" might work assuming that I can get in and close it correctly before it becomes a dupe of NRE again. I'll give that a shot next time. Another option might be to edit the question myself (including your suggested "Root cause is duplicate of ...." note) might work.
    – user3559349
    Commented Jan 20, 2016 at 23:37
  • 3
    0 scored duplicates are never deleted by roomba.
    – Braiam
    Commented Jan 21, 2016 at 13:10
  • @Braiam thanks. Good point on 0 scored dups. I've fixed my statement to align with meta.stackexchange.com/questions/78048/…. Commented Jan 21, 2016 at 17:27

You must log in to answer this question.