Error C# .Net "Value Cannot Be Null"

How is Model.MembershipTypes defined? A list? An Array? Are you sure there is anything IN the object?

It would probably behoove you to do some basic debugging on the page and ensure the data is what you’re expecting it to be (hint: it’s most likely not)

its defined as a list.

I try adding a breakpoint to see where the error is coming from but it did not work.

So start with adding a break before the view is run and check to ensure everything is as you expect it.

Eveything works fine but at that point it gives an error message saying value cannot be null. It is a register page and the part where the errror is the select list,

I was able to download, open, build and execute the application. I will get back to it later. But note that we should not need to download a 112 MB project.

3 Likes

Thank you samuel. I promise not to make this happen again

Is the following the page with the error? It seems to work for me.

Yes after you fill out the info and click register then the error appear.

This is what I have done so far.

Obviously the error is happening in the post-back, since it happens after pressing the button.

So I put a breakpoint at line 165 of AccountController.cs, the line with:

if (ModelState.IsValid)

Then I single-stepped from there. When the UserManager.CreateAsync succeeds the function returns but initially the function failed. So AddErrors was called. In the error message it was complaining about the password. When I finally got a password that was acceptable (Qwerty7&) registration succeeded. I hope that is a big clue. The problem is actually happening in the post-back of the form.

is there way i can fix this? What should i do?

Normally a developer would analyze what is happening, usually using the debugger to do analysis.

When I put a breakpoint on the line that has return View(model); (at the bottom of Register(RegisterViewModel model)) and when the breakpoint is hit then model.MembershipTypes is null. I do not know if that is the problem.

Thanks alot for your help samuel. I am not learning this and tried research ways how to fix it and still could not.

I really think that the problem is that MembershipTypes is null in the post-back but I do not know how that is happening. Evidently it is giving the error for the DropDownList because it uses MembershipTypes.

Do you know how post-backs work? If you do not then you need to learn that much at least.

There is a good chance the problem and solution is described in Model Binding To A List. The problem is that MembershipType is a complex type and the model binder can get confused. So I am not sure but if I understand then you need to have an index for each MembershipType in MembershipTypes. Oh, but you do; there is an Id in MembershipType. So I do not know, but for some reason MembershipTypes is null in the model for the postback. Except when debugging I see another model in the view that the MembershipTypes is not null.

Also, I see that you took a sample for a books website and you are trying to convert it to be a website for rentals. If that is correct then it would be better to start from an empty website and build from there. That way you can learn how things work. As it is there is too much that is overwhelming. I know the feeling; I know I can feel overwhelmed and not know where to look.

Samuel, I am following instruction from a video. I did everything exactly as mention but for some reason its giving this error. Here is the orginal source code of the entire application:

Okay well just compare the differences in the original code and your version.

In the original code before the return View(model); there is:

            using (var db = ApplicationDbContext.Create())
            {
                model.MembershipTypes = db.MembershipTypes.ToList();
            }

Put that (back) in your version and it works. Right?

1 Like

Thank you samuel. Do you have any resource that can help me learn to debug and get better at this?

I do not know, I really do not. I am learning ASP.Net too and I get frustrated with the tutorials and such. I have not tried books but I get frustrated with most books on other subjects.

One thing that makes a difference is your experience with other computer stuff. I have been programming for nearly half a century so I am experienced with debugging in general. It will help for you to know how to analyze problems methodically and by eliminating assumptions.

Something that will help you is to be able to ask questions when you need to. In my opinion the documentation that people create these days are not as good as it can be. I was a little rough on you about your questions here. I apologize if I was to rough. Normally it is best to not upload a lot of source code and/or require us to download a lot. Perhaps in this instance it was necessary. It will help for you to learn how to provide all the information that can be provided without providing much that is not needed and getting a balance can be difficult.

For me, I often learn by choosing something to do then figuring out how to do it. That might not be possible for a beginner when there is so much to learn and it is overwhelming.

I think that online environments are especially difficult to learn for beginners since it is difficult to know when things happen. Your question here is an example; the problem was actually in the post-back and that was not obvious. If you have not done a lot of non-interactive programming, such as console programs, then it might help to learn about debugging using an environment that is simpler than ASP.Net.

ASP.Net can be frustrating because there are so many libraries and such and they might be a problem and it might be difficult to know if that is the problem.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.