2015-09-28 40 views
0
private Employee searchForEmployee(int ID) 
{ 
    var EmployeeDetails = (from emp in EmployeeArray 
          where emp.m_EmployeeId == ID 
          select emp).FirstOrDefault(); 
    if (EmployeeDetails != null) 
    { 
     return EmployeeDetails; 
    } 
    else 
    { 
     return null; 
    } 
} 

问题(对不起,坏的格式,进出口新的这个和LINQ):创建使用LINQ一个搜索栏总是崩溃

我们似乎让所有的信息时,该ID的比赛,但是当没有匹配的ID程序只是崩溃,并给我们以下错误:

Additional information: Object reference not set to an instance of an object. If there is a handler for this exception, the program may be safely continued.

请帮助!

+2

也许,因为你返回'null'? – Backs

+0

您如何使用此方法的返回值? –

+0

这段代码没有任何错误(没有什么会引发异常)。如果某事调用'searchForEmployee'并尝试引用返回的'null'的属性,则会发生此异常。 –

回答

1

We seem to be getting all the information when the ID matches, but when there is no matching ID the program just crashes and gives us the following error.

这很有道理。给定您的代码,如果与给定的Employee不匹配,则返回null。因此,如果你这样做:

var employee = SearchForEmployee(1); 

// Attempting to access the Id propery on a null value will throw. 
Console.WriteLine(employee.Id); 

然后将通过一个RE,因为返回值是null。你需要一个空检查添加到您的代码:

var employee = SearchForEmployee(1); 
if (employee != null) 
    Console.WriteLine(employee.Id); 

或者,你可以使用C#-6空条件运算符:

var employee = SearchForEmployee(1); 
Console.WriteLine(employee?.Id); 

附注 - 您null检查里面SearchForEmployee是多余的,因为你无论如何,如果没有匹配,请返回null。这将做到:

private Employee SearchForEmployee(int Id) 
{ 
    return (from emp in EmployeeArray 
    where emp.m_EmployeeId == Id 
    select emp).FirstOrDefault(); 
} 

再或者,使用C#-6:

private Employee SearchForEmployee(int Id) => 
          EmployeeArray.FirstOrDefault(emp => emp.m_EmployeeId == Id); 

编辑

从评论:

Looks like this: private Employee[] EmployeeArray = new Employee[50]; It is created when the windows form loads up, and it is only initialized when an employee is registered. We are using this method after atleast one employee was added.

嗯,你唯一的初始化该数组,但不包含存储在该数组内的引用。这意味着你可能有一个Employee对象在那里初始化,但你有另外49个不是。

你有两个选择,要么修改查询到包括null检查:

private Employee SearchForEmployee(int Id) 
{ 
    return EmployeeArray.FirstOrDefault(emp => emp != null && emp.m_EmployeeId == Id); 
} 

或者你可以使用一个List<Employee>代替,这意味着它只会包含您已经添加了员工,它会随着您向其中添加更多员工而动态调整大小,从您的最终没有额外的工作。

+0

嘿,谢谢你这么快回复。 我应该在哪里放这个?当它到达“where”部分时,它会一直崩溃。 我应该在它之前运行一个空检查吗?如果是这样,应该如何写? 编辑: 只是刷新,我看到你写了更多的东西,我看着它。 –

+0

@OzFranko如果它在你的where子句中崩溃,那么你的数组可能不会被初始化。你如何实例化EmployeeArray? –

+0

看起来像这样: private Employee [] EmployeeArray = new Employee [50]; 它是在Windows窗体加载时创建的,只有在员工注册时才会初始化。 我们在至少添加一名员工后使用此方法。 –

0

您发布的方法不会引发错误。在代码中的某处,您正在调用返回null的方法。然后你试图访问它的一个属性,并且由于该对象为空,你会得到该错误。例如,你可能有这样的事情:

Employee emp = searchForEmployee(18); 
string name = emp.Name; //This will throw an error if no employee exists with id of 18 

当你需要检查,如果EMP不为空第一次作为这样的结果:

if(emp != null) 
{ 
    string name = emp.Name; 
} 

当然我只是猜测,如果名称是财产,但你应该明白这一点。你也可以做的是改变方法,所以它返回一个默认的员工对象,如果你不是返回null。这样的事情:

private Employee searchForEmployee(int ID) 
{ 
    var EmployeeDetails = (from emp in EmployeeArray 
          where emp.m_EmployeeId == ID 
          select emp).FirstOrDefault(); 
    if (EmployeeDetails != null) 
    { 
     return EmployeeDetails; 
    } 
    else 
    { 
     return new Employee 
     { 
      ID = 0, 
      Name = "default", //etc 
     }; 
    } 
}