Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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:
VB
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


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.
Posted
Updated 21-May-12 10:31am
v3
Comments
Maciej Los 21-May-12 16:47pm    
Try to debug your program on Form.Closed event.
Sergey Alexandrovich Kryukov 21-May-12 17:23pm    
Even if an exception could be in a partial class, it has nothing to do with the fact that a class is partial; this is totally irrelevant.
This exception is one of the easiest to detect; just use the debugger.
--SA
Tshuks 15-Nov-12 13:52pm    
Private Sub BSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BSave.Click

Main = FrmMain
If FormState = Form_State.Add_RecordState Then
If RequiredEntry() = True Then
Return
End If
Try

Dim DtaRow As DataRow

Dim MemStream As New MemoryStream
Dim DataPic_Insert As Byte()

DtaRow = DtaSet.Tables("Offenses").NewRow

With DtaRow
.Item("Vehicle_reg") = IIf(TxtVehicle_reg.Text = "", System.DBNull.Value, TxtVehicle_reg.Text)
.Item("First_Name") = IIf(TxtFirst_Name.Text = "", System.DBNull.Value, TxtFirst_Name.Text)
.Item("Last_Name") = IIf(TxtLast_Name.Text = "", System.DBNull.Value, TxtLast_Name.Text)
.Item("Title") = IIf(TxtTitle.Text = "", System.DBNull.Value, TxtTitle.Text)
.Item("Gender") = IIf(CBGender.Text = "", System.DBNull.Value, CBGender.Text)
.Item("Today_Date") = IIf(DTToday_Date.Text = "", System.DBNull.Value, DTToday_Date.Text)
.Item("NationalID") = IIf(TxtNationalID.Text = "", System.DBNull.Value, TxtNationalID.Text)
.Item("City") = IIf(TxtCity.Text = "", System.DBNull.Value, TxtCity.Text)
.Item("Province") = IIf(TxtProvince.Text = "", System.DBNull.Value, TxtProvince.Text)
.Item("Age") = IIf(TxtAge.Text = "", System.DBNull.Value, TxtAge.Text)
.Item("Offense") = IIf(TxtOffense.Text = "", System.DBNull.Value, TxtOffense.Text)
.Item("Description") = IIf(TxtDescription.Text = "", System.DBNull.Value, TxtDescription.Text)

Me.PicOffender.Image.Save(MemStream, Imaging.ImageFormat.Jpeg)
DataPic_Insert = MemStream.GetBuffer()
MemStream.Read(DataPic_Insert, 0, MemStream.Length)

.Item("OffenderPic") = DataPic_Insert
End With


what causes the "Object reference is not set to an instance of an object"

1 solution

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.

VB
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


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900