你调用的对象是空的.由局部类引起. [英] Object reference not set to an instance of an object. Caused By Partial Class.

查看:78
本文介绍了你调用的对象是空的.由局部类引起.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个简单的问题,但是由于我对课程的经验不足,所以我不确定到底是怎么回事.

发生的是,我创建了一个名为GetFullName的只读属性,代码如下:

Hello, I have what I believe is a simple problem, but due to my inexperience with classes, I''m not sure exactly what is going on.

What happens is, I created a read-only property called GetFullName and the code is as followed:

Partial Public Class PurchaseOrder
 
Public ReadOnly Property GetEmpFullName
Get
Dim EmpFullName As String = Employee.FirstName & " " & Employee.LastName
Return EmpFullName
End Get
End Property
 
End Class



它是针对员工的部分课程.而且,如果有关系,我正在使用Entity Framework来处理数据.

在我的表单上,我有一个组合框,用于显示员工的全名,并使用类作为显示成员,而其ID是用于保存到其他表中的值成员.

但是,每当我关闭表单时,都会出现错误,它会直接跳到我的局部类并突出显示我的GetFullName代码.

所以我的问题是,有谁确切知道我为什么会出错以及如何解决我的问题?感谢您阅读本文,也非常感谢您提供的所有帮助.

注意:该类工作正常,并且"Employee"指向ADO.NET实体数据模型中的employee表.仅在窗体关闭时才会出现此问题.

更新:
我的组合框指向员工的偏类GetFullName属性,但是采购订单的GetFullName属性是错误的出处.我做了很多思考,我认为这只是我将课堂整合在一起的方式.

我是否可以为整个实体创建一个类,然后仅引用我需要的表?这样,我就不会有重复的代码和多个类,这应该可以解决我的问题.



Its a partial class for employees. And if it matters, I''m using Entity Framework for handling data.

On my form I have a combo box that display the employees full name and I use the class for the display member, while its ID is the value member which is used for saving into other tables.

However whenever I close my form I get an error and it jumps straight to my partial class and highlights my GetFullName code.

So my question is, does anyone know exactly why I''m getting an error and what I can do to fix my problem? Thanks for reading this and much thanks for any and all help.

Note: The class works fine and "Employee" is pointing to the employee table in the ADO.NET Entity Data Model. The problem only occurs when the form closes.

Update:
My combo box is pointing to the employee''s partial class GetFullName property, but the purchase order''s GetFullName property is the one where the error is coming from. I''ve done a lot of thinking and I think its just the way I put my classes together.

Am I able to create a class just for the entities as a whole and then just reference the the tables I need to? That way I won''t have duplicate code and multiple classes and this should fix my problem.

推荐答案

错误是对象引用未设置为目的".这很可能是Employee对象.
因此,解决方案的一部分是检查Employee.如果Employee什么都不是,则返回一个空字符串.这样可以防止异常.

The error is "Object reference not set to an instance of an object". This will most likely be the Employee object.
So one part of the solution is to check Employee. If Employee is nothing then return an empty string. This will prevent the exception.

Public ReadOnly Property GetEmpFullName
Get
  If Employee Is Nothing Then
    Return String.Empty
  Else
    Dim EmpFullName As String = Employee.FirstName & " " & Employee.LastName
    Return EmpFullName
  End If
End Get
End Property



我不知道为什么Employee可能为空,但是由于这是在关闭表单时发生的,因此它最有可能与关闭和处置对象有关.
也许雇员在处置组合框之前就被处置了.



I do not know why Employee may be empty, but since this occurs when closing the form it has most likely to do with closing and disposing objects.
Perhaps Employee is disposed before the combo box is disposed.


这篇关于你调用的对象是空的.由局部类引起.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆