0

List<System.Drawing.Color>正如我所提到的,我有一个带有 的模型,它可以Seed()像这样使用:

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

只要我有Colors那样的那边,我总是 Object reference not set to an instance of an object.在 line收到var somethings = new List<Something>

当我删除 时Colors,它消失了,一切正常,是什么原因造成的,我该如何解决这个问题?

谢谢。

编辑:

Something的型号:

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; }
    }
}

控制器:

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

    //
    // GET: /Home/

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

查看:(我确信它是无关紧要的,因为调试器显示它Colors是空的。

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

全球.asax

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

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

        Database.SetInitializer<DatabaseContext>(new DatabaseInitializer());
    }
4

2 回答 2

3

您的Something构造函数可能会返回而不将Colors属性设置为空列表。您的集合初始化程序只是调用Colors.Add(Color.Black)然后- 如果属性返回空引用Colors.Add(Color.Red),那将不起作用。Colors

要么将其设置为一个空列表以开始(例如在构造函数中),要么创建一个新列表并设置属性本身:

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

了解上述代码与原始代码之间的区别很重要。您的代码当前等效于(在List<Something>集合初始化程序中):

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.

我上面的代码相当于:

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

看到不同?

于 2011-10-27T22:42:48.730 回答
1

在您的 Something 对象的构造函数中,确保您正在实例化一个新对象 Colors。我的猜测是你的问题。您没有发布太多可以使用的代码。

像这样的东西:

public Something()
{
     this.Colors = new List<Color>();
}

这样,您将始终在 Something 对象中拥有一个有效的列表对象。

好的,将您的模型代码更改为如下所示:

namespace MVCApplication7
{
    public class Something
    {
        public int SomethingID { get; set; }
        public string Name { get; set; }
        public List<Color> Colors { get; set; }
        public Something()
        {
            this.Colors = new List<Color>(); 
        }
    }
}

这将在您每次创建新的对象时实例化一个新的颜色列表,并防止对象引用错误。

更新 好的,我上面列出的模型是您对原始问题的解决方案:

var list = new List<Something>()
{
   new Something(){SomethingID = 1,Name="John", Colors = {Color.Red,Color.Black}},
   new Something(){SomethingID = 2,Name="George", Colors = {Color.Bisque,Color.Blue}},
   new Something(){SomethingID = 3,Name="Chris", Colors ={Color.Khaki,Color.Cornsilk}}
};
foreach (var item in list)
{
    Console.WriteLine(item.Name);
    foreach (var color in item.Colors)
    {
        Console.WriteLine(color.ToString());
    }
    Console.WriteLine("");
}

您可以看到每个 Something 对象都有自己独特的颜色列表。如果这解决了您的问题,请将此标记为答案。

于 2011-10-27T22:43:59.607 回答