使用 LINQ 创建搜索栏不断崩溃
Creating a searchbar using LINQ keeps crashing
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 都是新手):
当 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.
请帮忙!
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
与他的 Id
不匹配,则您 return null
。因此,如果您这样做:
var employee = SearchForEmployee(1);
// Attempting to access the Id propery on a null value will throw.
Console.WriteLine(employee.Id);
然后将通过 NRE,因为 return 值为 null
。您需要在代码中添加空检查:
var employee = SearchForEmployee(1);
if (employee != null)
Console.WriteLine(employee.Id);
或者,您可以使用 C#-6 空条件运算符:
var employee = SearchForEmployee(1);
Console.WriteLine(employee?.Id);
旁注 - 您在 SearchForEmployee
中的 null
检查是多余的,因为如果没有匹配,您 return 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>
,这意味着它只会包含您已添加的员工,并且会在您向其中添加更多员工时动态调整大小,而无需您进行额外的工作.
您发布的方法没有引发错误。在您的代码中的某处,您正在调用该方法,该方法为 returning 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;
}
当然,我只是在猜测 name 是否是 属性,但您应该明白我的意思。您还可以更改方法,如果您不想 return null,则它 return 是默认的员工对象。像这样:
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
};
}
}
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 都是新手):
当 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.
请帮忙!
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
与他的 Id
不匹配,则您 return null
。因此,如果您这样做:
var employee = SearchForEmployee(1);
// Attempting to access the Id propery on a null value will throw.
Console.WriteLine(employee.Id);
然后将通过 NRE,因为 return 值为 null
。您需要在代码中添加空检查:
var employee = SearchForEmployee(1);
if (employee != null)
Console.WriteLine(employee.Id);
或者,您可以使用 C#-6 空条件运算符:
var employee = SearchForEmployee(1);
Console.WriteLine(employee?.Id);
旁注 - 您在 SearchForEmployee
中的 null
检查是多余的,因为如果没有匹配,您 return 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>
,这意味着它只会包含您已添加的员工,并且会在您向其中添加更多员工时动态调整大小,而无需您进行额外的工作.
您发布的方法没有引发错误。在您的代码中的某处,您正在调用该方法,该方法为 returning 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;
}
当然,我只是在猜测 name 是否是 属性,但您应该明白我的意思。您还可以更改方法,如果您不想 return null,则它 return 是默认的员工对象。像这样:
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
};
}
}