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

  • SEARCH
  • Home
  • 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 6730003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:21:24+00:00 2026-05-26T10:21:24+00:00

I have a model with a List<System.Drawing.Color> , as I mentioned, which comes to

  • 0

I have a model with a List<System.Drawing.Color>, as I mentioned, which comes to use in the Seed() like this:

    protected override void Seed(DatabaseContext context)
    {
        var somethings = new List<Something>
        {
            new Something
            {
                Name="blah blah", Colors= { Color.Black, Color.Red }
            }
        };
    }

And as long as I have the Colors over there like that, I always recieve
Object reference not set to an instance of an object. at line var somethings = new List<Something>.

When I remove the Colors, it goes away and everything works perfectly, what causes this and how can I solve this problem?

Thanks.

EDIT:

Something‘s model:

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

namespace MVCApplication7
{
    public class Something
    {
        public int SomethingID { get; set; }
        public string Name { get; set; }
        public List<Color> Colors { get; set; }
    }
}

Controller:

    ........
    private DatabaseContext db = new DatabaseContext();

    //
    // GET: /Home/

    public ViewResult Index()
    {
        return View(db.Somethings.ToList());
    }

View: (I’m sure it’s irrelavent because the debugger shows the Colors is empty.

        @foreach (var itemColor in item.Colors)
        {
            Html.Raw(itemColor.ToString());
        }

Global.asax

    .........
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        Database.SetInitializer<DatabaseContext>(new DatabaseInitializer());
    }
  • 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-26T10:21:24+00:00Added an answer on May 26, 2026 at 10:21 am

    Chances are your Something constructor is returning without setting the Colors property to an empty list. Your collection initializer is just calling Colors.Add(Color.Black) and then Colors.Add(Color.Red) – that’s not going to work if the Colors property is returning a null reference.

    Either set it to an empty list to start with (e.g. in the constructor) or create a new list and set the property itself:

    new Something
    {
        Name = "blah blah",
        Colors = new List<Color> { Color.Black, Color.Red }
    }
    

    It’s important that you understand the difference between the above code and your original. Your code is currently equivalent to (within the List<Something> collection initializer):

    Something tmp = new Something();
    tmp.Name = "blah blah";
    tmp.Colors.Add(Color.Black);
    tmp.Colors.Add(Color.Red);
    // Now add tmp to the list we're building.
    

    My code above is equivalent to:

    Something tmp = new Something();
    tmp.Name = "blah blah";
    List<Color> tmp2 = new List<Color>();
    tmp2.Add(Color.Black);
    tmp2.Add(Color.Red);
    tmp.Colors = tmp2;
    // Now add tmp to the list we're building
    

    See the difference?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the dropdown list like this <?php echo $form->dropdownList($customers,'customer_name', CHtml::listData(Customers::model()->findAll(), 'id', 'customer_name'), array(
Hay I have a model like this def Photo(models.Model): # Photo object fields... def
I have a list of viewmodels which contains vehicle data such as this public
I have a system which is using Spring for dependency injection. I use annotation-based
I have the following: MODEL.PY LIST = (('Manager', 'Manager'),('Non-Manager', 'Non-Manager'),) class Employee(models.Model): fname =
I have a Model that contains a List of a custom type. I want
I have problem with keeping my model (represented as List) in sync with a
In my view model if have a: List<Car> where car has an Id and
In my Core Data model I have two entities: List and Patient. List has
Lets say a have the following Model public class FirstModel { public List<SecondModel> SecondModels

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.