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.ラインで受け取り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());
        }

Global.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)ているだけで、プロパティが null 参照を返して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>(); 
        }
    }
}

これにより、新しい何かオブジェクトを作成するたびに新しい色のリストがインスタンス化され、オブジェクト参照エラーが防止されます。

更新 あなたのモデルとして上にリストしたものでOKです。元の質問に対する解決策は次のとおりです。

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 に答える