我不知道是什么问题,它给人的错误是:“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();
}
}
}
参见:http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in -净 – 2013-02-20 20:42:07