2013-02-20 61 views
-4

我不知道是什么问题,它给人的错误是:“System.NullReferenceException

类型的未处理的异常‘System.NullReferenceException’ 发生在AlgoAssignment.exe其他信息:对象 引用未设置为对象的实例。

这是代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
namespace AlgoAssignment 
{ 
    class person 
    { 
     static int id; 
     public List<string> prefrence { get; set; } 
     public string name { get; set; } 
     public string engagedTo; 
     public void setName(string a) { this.name = a; } 
     public void setPrefrence(string a) 
     { 
      this.prefrence.Add(a); 
    } 
    public person() 
    { 
     id++; 
     name = null; 
     engagedTo = null; 

    } 
    public void displayInfo() 
    { 
     Console.Write("Name : " + this.name + "\n"); 
     Console.Write(this.prefrence); 

    } 

} 
class Program 
{ 
    static void Main(string[] args) 
    { 
     string fileName = "A1P2in1a.txt"; 
     StreamReader sr = new StreamReader(fileName); 
     string no = sr.ReadLine();  // total use cases 
     int n = int.Parse(no); 
     sr.ReadLine(); 


     person[] pr = new person[n]; 


     for (int i = 0; i < n; i++) 
     { 
      string newLine = sr.ReadLine(); 
      string[] parts = newLine.Split(' '); 
      parts[0] = parts[0].Replace(":", ""); 
      pr[i].setName(parts[0]); 
      for (int j = 1; j < n; j++) 
      { 
       pr[i].setPrefrence(parts[j]); 
      } 
     } 

     for (int i = 0; i < n; i++) 
     { 
      pr[i].displayInfo(); 
     } 


     Console.ReadKey(); 



    } 
} 

}

+1

参见:http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in -净 – 2013-02-20 20:42:07

回答

2

你不是在访问之前在实例化数组人。

person[] pr = new person[n]; 

    for (int i = 0; i < n; i++) 
    { 
     string newLine = sr.ReadLine(); 
     string[] parts = newLine.Split(' '); 
     parts[0] = parts[0].Replace(":", ""); 
     pr[i] = new person(); // add this <---- 
     pr[i].setName(parts[0]); 
     for (int j = 1; j < n; j++) 
     { 
      pr[i].setPrefrence(parts[j]); 
     } 
    } 

而且我认为这将是更好地创造新的人,对其进行初始化,然后才分配给数组项:

person[] pr = new person[n]; 

    for (int i = 0; i < n; i++)   
     pr[i] = CreatePersonFrom(sr.ReadLine(), n); 

这里是人创造:

private static person CreatePersonFrom(string newLine, int preferencesCount) 
{ 
    person p = new person(); 
    string[] parts = newLine.Split(' '); 
    p.setName(parts[0].Replace(":", "")); 

    for(int i = 1; i < preferencesCount; i++) 
     p.setPrefrence(parts[i]); 

    return p; 
} 

在C#中,我们使用PascalCase名称作为类和方法。

2

您在为其分配值之前访问preference。在你的构造做到这一点:

public person() 
{ 
    id++; 
    name = null; 
    engagedTo = null; 
    preference = new List<string>(); // <- Add this to initialize preference. 
} 

您也正在运行到同样的问题与你的person[] pr阵列。你初始化这个数组,但不是里面的任何人。

for (int i = 0; i < n; i++) 
{ 
    // ... 
    pr[i] = new person(); // This needs to be there before getting/setting pr[i] 
    // ... 
} 

您可能也想看看.NET Naming Conventions特别是在关于class namingmethod naming

相关问题