对于我的代码,有3个列表集合。一个持有Employees(带有Name / ID参数)和服务请求(包含参数name / id / description)的用户。然后第三个是分配,其中包含来自其他2个列表的员工和服务请求(将员工分配给服务请求)。
我正在阅读为员工/服务请求工作但是对于分配我得到错误“对象引用未设置为对象的实例”。当我运行我的代码时,我不确定如何修复它。
assignment.Employee.Name = columns[0];
处的错误点我认为将整个员工对象分配到列[0]可能类似于//assignment.Employee = columns[0];
。但是它给出了我无法将字符串转换为MaintenanceManagementSystem.Employee的错误。
/阅读员工的代码。
public static List<Employee> GetEmployees()
{
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
StreamReader textIn =
new StreamReader(
new FileStream(path1, FileMode.OpenOrCreate, FileAccess.Read));
List<Employee> employees = new List<Employee>();
while (textIn.Peek() != -1)
{
string row = textIn.ReadLine();
string[] columns = row.Split('|');
Employee employee = new Employee();
employee.Name = columns[0];
employee.ID = columns[1];
employees.Add(employee);
}
textIn.Close();
return employees;
}
/读取请求的代码
public static List<ServiceRequest> GetRequest()
{
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
StreamReader textIn =
new StreamReader(
new FileStream(path2, FileMode.OpenOrCreate, FileAccess.Read));
List<ServiceRequest> serviceRequests = new List<ServiceRequest>();
while (textIn.Peek() != -1)
{
string row = textIn.ReadLine();
string[] columns = row.Split('|');
ServiceRequest serviceRequest = new ServiceRequest();
serviceRequest.Name = columns[0];
serviceRequest.ID = columns[1];
serviceRequest.Desc = columns[2];
serviceRequests.Add(serviceRequest);
}
textIn.Close();
return serviceRequests;
}
/阅读作业代码
public static List<Assignment> GetAssignment()
{
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
StreamReader textIn =
new StreamReader(
new FileStream(path3, FileMode.OpenOrCreate, FileAccess.Read));
List<Assignment> assignments = new List<Assignment>();
while (textIn.Peek() != -1)
{
string row = textIn.ReadLine();
string[] columns = row.Split('|');
Assignment assignment = new Assignment();
//Employee employee = new Employee();
//assignment.Employee = columns[0];
assignment.Employee.Name = columns[0];
assignment.Employee.ID = columns[1];
//assignment.Request = columns[1];
assignment.Request.Name = columns[2];
assignment.Request.ID = columns[3];
assignment.Request.Desc = columns[4];
assignments.Add(assignment);
}
textIn.Close();
return assignments;
}
}
//表单加载时从文本文件中读取的代码
private void ManagerStart_Load(object sender, EventArgs e)
{
myAssignments.Employees = FileIO.GetEmployees();
myRequest.Requests = FileIO.GetRequest();
myAssignments.Assignments = FileIO.GetAssignment();
lstEmployee.Items.Clear();
lstServiceRequest.Items.Clear();
foreach (Employee employee in myAssignments.Employees)
{
lstEmployee.Items.Add(employee.ToString());
}
foreach (ServiceRequest serviceRequest in myRequest.Requests)
{
lstServiceRequest.Items.Add(serviceRequest.ToString());
}
foreach (Assignment assignment in myAssignments.Assignments)
{
lstAssignments.Items.Add(assignment.ToString());
}
}
答案 0 :(得分:1)
您必须在Employee
构造函数或外部创建属于Request
的{{1}}和Assignment
的实例。
Assignment
或
public class Assignment
{
public Employee Employee { get; set; }
public Request Request { get; set; }
public Assignment()
{
Employee = new Employee();
Request = new Request();
}
}