需要帮助来弄清楚我的代码有什么问题。 我收到此错误: System.NullReferenceException:'对象引用未设置为对象的实例。'在这一行:
post[0].Author = "Author at post 0";
守则:
class Logg
{
public string Title { get; set; }
public string Text { get; set; }
public string Author { get; set; }
public DateTime Date { get; set; }
public static int Count = 0; //Count += 1 from Main
}
class Program
{
static void Main(string[] args)
{
List<Logg[]> LoggBok = new List<Logg[]>();
Logg[] post = new Logg[10];
LoggBok.Add(post);
post[0].Author = "Author at post 0";
post[0].Title = "Titel at post 0";
post[0].Text = "My text at post 0";
post[0].Date = DateTime.Now;
post[1].Author = "Author 1 at post 1";
post[1].Title = "Titel 1 at post 1";
post[1].Text = "My text 1 at post 1";
post[1].Date = DateTime.Now;
post[2].Author = "Author 2 at post 2";
post[2].Title = "Titel 2 at post 2";
post[2].Text = "My text 2 at post 2";
post[2].Date = DateTime.Now;
Logg.Count = 3;
答案 0 :(得分:1)
Logg[] post = new Logg[10];
创建了包含10个空值的数组,现在您必须为每个空值分配内存,例如:
post[0] = new Logg();