2017-04-11 55 views
-2

需要帮助来弄清楚我的代码出了什么问题。 我收到此错误: System.NullReferenceException:'对象引用未设置为对象的实例。'在这条线:Smth与索引错误(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

PLZ不要downvote,我是新来的主题和答案像“这是一个重复”等不会帮助,我已经阅读其他职位有同样的错误,但仍然困惑。 – Gusto

+1

刚刚接触这个主题并没有改变Stack Overflow不需要另一个“我的引用为空,为什么?”题。至少,您需要以比“更需要帮助弄清楚我的代码出了什么问题”的更精确的方式陈述您的问题。告诉我们你到目前为止所做的调试,以及具体怎样解决问题。详细解释您在该主题的规范文章中所遵循的步骤(请参阅标记的副本),以及这些步骤如何失败。 –

+0

@PeterDuniho,好的,那么WWW上有没有其他平台可以发布这样的虚拟问题?我会去那里,然后问我那里。 – Gusto

回答

1
Logg[] post = new Logg[10]; 

与10个空创建阵列,现在你必须为他们每个人分配内存,如:

post[0] = new Logg();