0
public Person[] GetColleagues(String fn, String ln)
{
    string[] data1 = File.ReadAllLines(@"link1")
    string[] personv = data1[0].Split('$');
    string[] data2 = File.ReadAllLines(@"link2");
    string[] jobv = data2[0].Split('$');

    string[] jobsearch = Array.FindAll(jobv, element => element.Contains(GetJobInfo(fn, ln).companyName));

     if (jobsearch.Length>0)
     {
         string[] colleagues = new string[jobsearch.Length];

         for (int n = 0; n < jobsearch.Length - 1; n++)
         {
             String[] pn = new String[jobsearch.Length];
             string[] detail = jobsearch[n].Split('#');

             pn[n] = detail[0];

             colleagues[n] = Array.Find(personv, element => element.EndsWith(pn[n]));}   

             Person[] instances = new Person[colleagues.Length];

             for (int i = 0; i < colleagues.Length-1; i++)
             {
                 string[] test = colleagues[i].Split('#');

                 instances[i].firstName = test[0];
                 instances[i].lastName = test[1];
                 instances[i].dateOfBirth = Convert.ToDateTime(test[2]);
                 instances[i].email = test[3];
                 instances[i].streetAddress = test[4];
                 instances[i].suburb = test[5];
                 instances[i].state = test[6];
                 instances[i].postcode = Convert.ToInt32(test[7]);
                 instances[i].job.positionNumber = Convert.ToInt32(test[8]);
                 instances[i].job.positionTitle = helpmethod(test[8])[1];
                 instances[i].job.positionDescription = helpmethod(test[8])[1];
                 instances[i].job.companyName = helpmethod(test[8])[1];
             }
             return instances;
         }
         return null;
}

public String[] helpmethod(string num)
{
    string[] data2 = File.ReadAllLines(@"link2");
    string[] jobv = data2[0].Split('$');
    string searchjob = Array.Find(jobv, element => element.StartsWith(num, StringComparison.InvariantCultureIgnoreCase));
    string[] resultjob = searchjob.Split('#');

    return resultjob;
}

我总是得到一个空引用异常空错误 instances[i].firstNameinstances[i].lastName...... 即使我更改 instances[i].firstName = test[0];instances[i].firstName = "tony"; 仍然得到空引用异常错误。

这是自定义我的人班和工作班

[DataContract]
public class Person
{
    [DataMember]
    public String firstName { get; set; }
    [DataMember]
    public String lastName { get; set; }
    [DataMember]
    public DateTime dateOfBirth { get; set; }
    [DataMember]
    public String email { get; set; }
    [DataMember]
    public String streetAddress { get; set; }
    [DataMember]
    public String suburb { get; set; }
    [DataMember]
    public String state { get; set; }
    [DataMember]
    public int postcode { get; set; }
    [DataMember]
    public Job job { get; set; }
}

[DataContract]
public class Job
{
    [DataMember]
    public String companyName { get; set; }
    [DataMember]
    public int positionNumber { get; set; }
    [DataMember]
    public String positionTitle { get; set; }
    [DataMember]
    public String positionDescription { get; set; }

}

这是我的txt文件

对于链接1

$abc#ko#18/07/1959#abc@gmail.com#unit 90,25 thor Street#unknow#Queensland#4109#0$Efg#Ans#9/11/1990#tt.y@gmail.com#223 Anson Rd#ChinaTown#Boston#4188#1$Catherine#Ong#05/05/1993#cth.ong@gmail.com#osh Street#Melbourne#Vic#2008#2$Kelvin#Ong#04/05/1983#k.ong@gmail.com#ash Street#hello#qld#4308#3$Amy#Goh#01/01/1987#a.goh@gmail.com#lol Street#hey#qld#4108#4$Henry#Li#06/06/1988#h.li@gmail.com#lucky Street#Chiantown#qld#4118#5

对于链接2

$0#Boss#God of this company#hi Technology Pte Ltd$1#Engineer#Engineer Leader#ABC Pte Ltd$2#Secretary#assist boss#hi Technology Pte Ltd$3#AssistEngineer#assist Engineer#hi Technology Pte Ltd$4#Cleaner#cleanning work#hi Technology Pte Ltd$5#CEO#Boss of this company#ABC Pte Ltd
4

2 回答 2

3

您像这样声明数组:

Person[] instances = new Person[colleagues.Length];

但是您会看到数组中的每个元素仍然null处于该点。您必须Person为每个元素创建一个新实例:

for (int i = 0; i < colleagues.Length-1; i++)
{
    string[] test = colleagues[i].Split('#');
    instances[i] = new Person();
    instances[i].firstName = test[0];
于 2013-09-17T12:08:46.470 回答
2

您需要初始化instances[i]

    instances[i] = new Person();        
    instances[i].job = new Job();
    instances[i].firstName = test[0];
于 2013-09-17T12:08:48.567 回答