Object reference not set to an instance of an object.'

Nishtha Bhagwan 1 Reputation point
2022-08-19T20:17:46.497+00:00

232940-screenshot-2022-08-19-221221.png232909-screenshot-2022-08-19-221333.png232988-screenshot-2022-08-19-221400.png

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using u21468992_HW04.ViewModels;

namespace u21468992_HW04.Controllers
{
public class VolunteerController : Controller
{
// GET: Volunteer
public ActionResult Index()
{
List <VolunteerViewModel> volunteers = GetVolunteerData();
return View(volunteers);
}
//Create test data
private List <VolunteerViewModel> GetVolunteerData()
{
List<VolunteerViewModel> volunteers = new List<VolunteerViewModel>();
VolunteerViewModel volunteer1 = new VolunteerViewModel("Name", "Surname", 0, "Area");
VolunteerViewModel volunteer2 = new VolunteerViewModel("Name", "Surname", 0, "Area");
VolunteerViewModel volunteer3 = new VolunteerViewModel("Name", "Surname", 0, "Area");
VolunteerViewModel volunteer4 = new VolunteerViewModel("Name", "Surname", 0, "Area");
VolunteerViewModel volunteer5 = new VolunteerViewModel("Name", "Surname", 0, "Area");
VolunteerViewModel volunteer6 = new VolunteerViewModel("Name", "Surname", 0, "Area");
VolunteerViewModel volunteer7 = new VolunteerViewModel("Name", "Surname", 0, "Area");
VolunteerViewModel volunteer8 = new VolunteerViewModel("Name", "Surname", 0, "Area");
VolunteerViewModel volunteer9 = new VolunteerViewModel("Name", "Surname", 0, "Area");
VolunteerViewModel volunteer10 = new VolunteerViewModel("Name", "Surname", 0, "Area");

        volunteers.Add(volunteer1);  
        volunteers.Add(volunteer2);  
        volunteers.Add(volunteer3);  
        volunteers.Add(volunteer4);  
        volunteers.Add(volunteer5);  
        volunteers.Add(volunteer6);  
        volunteers.Add(volunteer7);  
        volunteers.Add(volunteer8);  
        volunteers.Add(volunteer9);  
        volunteers.Add(volunteer10);  
        return volunteers;  
    }  
}  

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace u21468992_HW04.ViewModels
{
public class VolunteerViewModel
{
//Define attributes
private string _Name;
private string _Surname;
private int _Contact;
private string _Area;

    //Constructor  
    public VolunteerViewModel(string name, string surname, int contact, string area) // called when a Volunteer view model is created  
    {  
        _Name = name;  
        _Surname = surname;  
        _Contact = contact;  
        _Area = area;  
    }  

    //Access modifiers - allows for encapsulation  
    public string Name  
    {  
        get { return _Name; }  
        set { _Name = value; }  
    }  

    public string Surname  
    {  
        get { return _Surname; }  
        set { _Surname = value; }  
    }  

    public int Contact  
    {  
        get { return _Contact; }  
        set { _Contact = value; }  
    }  

    public string Area  
    {  
        get { return _Area; }  
        set { _Area = value; }  
    }  
}  

}

@默 List<u21468992_HW04.ViewModels.VolunteerViewModel>
@{

    ViewBag.Title = "Volunteers";  

}
<h2>Volunteers</h2>
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Contact</th>
<th>Area</th>
</tr>

@foreach (var volunteer in Model)
{
<tr>
<td>@volunteer.Name</td>
<td>@volunteer.Surname</td>
<td>@volunteer.Contact</td>
<td>@volunteer.Area</td>
</tr>
}

</table>

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,309 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,386 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 50,426 Reputation points
    2022-08-19T21:24:44.847+00:00

    Please use the Code Sample button in the editor to post code. Screenshots of code do not help us as we cannot copy the code to try it ourselves. Thanks.

    While you provided the code, you didn't provide us the context of when you're getting this error. So we're guessing at what you're doing to get here.

    One possible scenario (at least with older versions of VS) is that you're in the designer on the cshtml file. You tell the debugger to run using F5. It brings up the browser and you get the error. This was an odd behavior of the debugger for quite a while. The workaround was to ensure that you didn't have a cshtml file active when you start the debugger. Just open a .cs file in the editor and try again.

    If that is not your case then my next guess is that your GetVolunteerData method isn't returning back anything. Since it is partial screenshot we have no way of knowing. Put a breakpoint on your return View(volunteers) line in Index and run the debugger. When it hits that line check the value of volunteers. If it is null then the issue is with the method.

    If you don't hit that breakpoint then this controller isn't actually being used. You have another controller that is being called instead. Unfortunately we have no way of knowing what that is. My first recommendation is to ensure your code is compiling. If you are getting any compiler errors then the debugger isn't running the code you have. Fix the errors. If it does compile then look at the call stack when the exception is thrown. I suspect it isn't getting called from where you think it is.

    Also be aware that models in MVC need to have a default constructor (in addition to any other constructor you define). If they don't then you cannot use them to pass data back from the browser to the server. The runtime cannot create an instance of the model and therefore will fail the call.

    0 comments No comments