2014-01-29 33 views
0

我知道这可能类似于其他一些职位,但我不太确定我在这里做错了什么。作为一个供参考,我是编程新手,仍然试图学习正确的流程。空引用异常是未处理的C#控制台应用程序

这里是代码,异常发生在“MyFriends [i] .Name = friendName”。

using System; 
using System.Collections; 

namespace FriendList 
{ 
    class FriendList 
    { 
     static public Friend[] MyFriends = new Friend[2]; 
     public static void Main() 
     { 
      string friendName; 
      string friendPhone, friendMonth, friendDay, friendYear; 
      int intMonth, intDay, intYear; 

      for (int i = 0; i < 2; ++i) 
      { 
       Console.Write("enter name"); 
       friendName = Console.ReadLine(); 
       MyFriends[i].Name = friendName; 

       Console.Write("phone"); 
       friendPhone = Console.ReadLine(); 
       MyFriends[i].Phone = friendPhone; 

       Console.WriteLine("Enter Month: "); 
       friendMonth = Console.ReadLine(); 
       intMonth = Convert.ToInt32(friendMonth); 
       MyFriends[i].Month = intMonth; 

       Console.WriteLine("Enter Day: "); 
       friendDay = Console.ReadLine(); 
       intDay = Convert.ToInt32(friendDay); 
       MyFriends[i].Day = intDay; 

       Console.WriteLine("Entery Year: "); 
       friendYear = Console.ReadLine(); 
       intYear = Convert.ToInt32(friendYear); 
       MyFriends[i].Year = intYear; 
      } 

      for (int i = 0; i < 2; ++i) 
      { 
       string information = string.Format("first name: {0}, phone {1}", MyFriends[i].Name, MyFriends[i].Phone); 
       Console.WriteLine(information); 
      } 
      Console.Read(); 
     } 
    } 

    class Friend 
    { 
     string _Name = string.Empty, _Phone = string.Empty; 
     int _Day = 0, _Month = 0, _Year = 0; 

     public string Name 
     { 
      get { return _Name; } 
      set { _Name = value; } 
     } 

     public string Phone 
     { 
      get { return _Phone; } 
      set { _Phone = value; } 
     } 

     public int Month 
     { 
      get { return _Month; } 
      set { _Month = value; } 
     } 

     public int Day 
     { 
      get{ return _Day; } 
      set{ _Day = value ; } 
     } 

     public int Year 
     { 
      get { return _Year;} 
      set { _Year = value; } 
     } 

     public Friend() 
     { } 

    } 
} 

谢谢你的指导!

+0

欢迎来到SO。你可以添加堆栈跟踪异常到你的问题? –

+0

这是什么迭代的循环呢?也就是说,“我”的价值是什么? – pamphlet

回答

5

你的朋友数组被初始化为空。因此,MyFriends[i]将命中空引用,这是另一种说法,你正试图访问不存在的东西。

换句话说,您有一个Array带两个朋友的插槽,但两个插槽都是空的。您还需要有一个朋友在每个时隙才可以使用它们的属性,如NamePhone

只需启动for循环是这样的:

for (int i = 0; i < 2; ++i) 
{ 
    MyFriend[i] = new Friend(); //or pass parameters as required by the constructor 
    // rest of your code goes here 
} 

,事情都会好起来的。这样,您就可以为您将要使用的插槽添加好友。

+1

感谢您的澄清。完美的作品! –

1

您已经创建了包含两个元素的数组,但您已将元素设置为任何值。他们都是null

static public Friend[] MyFriends = new Friend[2]; 

所以,当您尝试使用MyFriends[i]从阵列中,你实际上得到null

MyFriends[i].Name = friendName; 

那你的NullReferenceException来自哪里。


您必须初始化数组的成员。例如:

for (int i = 0; i < MyFriends.Length; i++) 
{ 
    // Put a new Friend object in the array. 
    MyFriends[i] = new Friend(); 

    // ... 
0

MyFriends是Friend类的一个数组。 数组中的每个元素都需要用一个朋友构造函数初始化,因此它将被分配一个内存。

1

创建集合时,它们将使用目标类型的默认值以及任何引用类型的默认值填充(如果null)。所以要解决你的问题,你必须在访问它们之前初始化阵列中的物品:

.... 
for (int i = 0; i < 2; ++i) 
{ 
    MyFriends[i] = new Friend(); 
    ... 
相关问题