为什么这个Application对象没有实例化?每当我运行代码时它都为null.
public class Applicant
{
private Application oApplication = new Application();
public Applicant()
{
oApplication = new Application();
}
public Application Application
{
get { return oApplication; }
set { oApplication = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
这是应用程序类
public class Application
{
public string ApplicationID { get; set; }
public ContactDetails ContactDetails { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是调用代码....
public Applicant[] GetApplicants()
{
Applicant[] oApplicant;
DataSet dsExcelSchema = new DataSet();
dsExcelSchema = GetDataAsDataSet();
DataTable contactInfoTable = dsExcelSchema.Tables["ContactInformation$"];
int numOfApplications = contactInfoTable.Rows.Count - 1;
int i = 0;
oApplicant = new Applicant[numOfApplications];
foreach (DataRow dr in contactInfoTable.Rows)
{
Application oApplication = new Application();
oApplicant[i].Application.ApplicationID = dr["ApplicationID"].ToString();
i++;
}
return oApplicant;
}
Run Code Online (Sandbox Code Playgroud)
它给了我一个NullReferenceException.
你忘了在数组元素中创建申请人的实例 - 应该这样做:
foreach (DataRow dr in contactInfoTable.Rows)
{
oApplicant[i] = new Applicant();
oApplicant[i].Application.ApplicationID = dr["ApplicationID"].ToString();
i++;
}
Run Code Online (Sandbox Code Playgroud)