Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6558849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:14:59+00:00 2026-05-25T13:14:59+00:00

I’m playing around with MVC3, trying to create small test projects which simulate problems

  • 0

I’m playing around with MVC3, trying to create small test projects which simulate problems that I anticipate I will encounter in a larger project I’m migrating from PHP. I keep getting a “NullReferenceException” on instantiating the “model” variable in the project with the elements below:

Model:

public class MainModel
{
    public IEnumerable<Chemical> chemicals { get; set; }

}

public class Chemical
{
    public int ChemicalId { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Hazard> Hazards { get; set; }

}

public class Hazard
{
    public int HazardId { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Chemical> Chemicals { get; set; }
}

Controller:

public class MainController : Controller
{

    public ActionResult Index()
    {

        var h1 = new Hazard { HazardId = 1, Description = "Flammable" };
        var h2 = new Hazard { HazardId = 2, Description = "Carcinogen" };
        var h3 = new Hazard { HazardId = 3, Description = "Water Reactive" };

        var model = new [] {
            new Chemical { ChemicalId = 1, Name = "Benzene", Hazards = {h1, h2}},
            new Chemical { ChemicalId = 2, Name = "Sodium", Hazards = { h3 } },
            new Chemical { ChemicalId = 3, Name = "Chloroform", Hazards =  { h2 } },
            new Chemical { ChemicalId = 4, Name = "Water" }
        }; //NULL EXCEPTION THROWN HERE

        return View(model);
    }

}

So, I have two questions:

  1. Why do I keep getting the NullReferenceException, and how could it be fixed?
  2. In writing this code, I used this StackOverFlow Page as a starting point. Why is this okay, but what I wrote is not? Granted, I didn’t test this code, but the author seems very competent.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:15:00+00:00Added an answer on May 25, 2026 at 1:15 pm

    Why do I keep getting the NullReferenceException, and how could it be
    fixed?

    The ICollection property Hazards is null initially – you have to use a constructor when you create your model:

    var model = new[] {
        new Chemical { ChemicalId = 1, Name = "Benzene", Hazards = new List<Hazard>() {h1, h2}},
        new Chemical { ChemicalId = 2, Name = "Sodium", Hazards = new List<Hazard>(){ h3 } },
        new Chemical { ChemicalId = 3, Name = "Chloroform", Hazards =  new List<Hazard>(){ h2 } },
        new Chemical { ChemicalId = 4, Name = "Water" }
    };
    

    The code you currently have will try to add i.e. {h1,h2} to Hazards – which is null and throw a NullReferenceException. An alternative is to create a new empty Hazards collection in the Chemical constructor – then you can leave your current code unchanged:

    public class Chemical
    {
        public Chemical()
        {
            _hazards = new List<Hazard>();
        }
        //..
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.