Error - Object reference not set to an instance of an object.

  • Thread starter Jessica Sterner via .NET 247
  • Start date
J

Jessica Sterner via .NET 247

I keep getting the error message: An unhandled exception of type 'System.NullReferenceException' occurred in Final2.exe

Additional information: Object reference not set to an instance of an object.

I am new to programming and am not sure what is wrong. I feel like I have tried everything. Any advice would be appreciated. Thanks.

This is the code in the class library:

Option Strict On
Option Explicit On

Imports System.IO
Imports System.Convert
Imports System.Windows.Forms

'Define the root namespace as Course. In the Course namespace,
'define another namespace named People. The People namespace, in turn,
'should have a class named Employees.
Namespace People
Public Class Employees

Public Structure Employee
Public ID As Integer
Public LastName As String
Public FirstName As String
Public Address As String
Public City As String
Public State As String
Public ZipCode As String
Public AnnualSales As Single
End Structure

'Declare a hidden array of Employee sructures so the developer can retrieve
'records from the array by calling defined methods.
Public pEmployee() As Employee

' The FileOk event handler fires if the user clicks the OK button on the OpenFileDialog.
' Attempt to open the file. Create a structured error handler to display a message if
' the file cannot be opened for some reason.
Public Sub New(ByVal FileName As String)
Dim pstrLine As String
Dim pintCount As Integer
Dim pstrFields() As String
Dim pchrDelimiter As Char() = {CChar(",")}


Try
Dim srdCurrent As New StreamReader(FileName)
pstrLine = srdCurrent.ReadLine
Do Until pstrLine = Nothing
ReDim Preserve pEmployee(pintCount)

' Divide the current line into individual fields.
pstrFields = pstrLine.Split(pchrDelimiter)
With pEmployee(pintCount)
.ID = ToInt32(pstrFields(0))
.LastName = pstrFields(1)
.FirstName = pstrFields(2)
.Address = pstrFields(3)
.City = pstrFields(4)
.State = pstrFields(5)
.ZipCode = pstrFields(6)
.AnnualSales = ToSingle(pstrFields(7))
End With
pintCount += 1

pstrLine = srdCurrent.ReadLine
Loop
srdCurrent.Close()

Catch ex As System.Exception

MessageBox.Show("Can't Load File", "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub

End Class
End Namespace

This is the code in the Main Form:

Option Strict On
Option Explicit On

Imports System.IO
Imports System.Convert
Imports Course.People

Public Class Form1
Inherits System.Windows.Forms.Form

Private EmpClass As Employees
Private EmpArray() As Employees.Employee

Public intCurrentRecord As Integer

Private Sub mmuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuFileOpen.Click
ofdMain.ShowDialog()
'open the file by creating an instance of the class.
EmpClass = New Employees(ofdMain.FileName)
Call ShowCurrentRecord(intCurrentRecord)

End Sub

' Display the current record.
Friend Sub ShowCurrentRecord(ByVal pintCurrent As Integer)
EmpArray(pintCurrent) = New Employees.Employee ' I just added this line to see if it would help with no luck.
txtIDNumber.Text = EmpArray(pintCurrent).ID.ToString
txtLastName.Text = EmpArray(pintCurrent).LastName
txtFirstName.Text = EmpArray(pintCurrent).FirstName
txtAddress.Text = EmpArray(pintCurrent).Address
txtCity.Text = EmpArray(pintCurrent).City
txtState.Text = EmpArray(pintCurrent).State
txtZipCode.Text = EmpArray(pintCurrent).ZipCode
txtAnnualSales.Text = EmpArray(pintCurrent).AnnualSales.ToString
End Sub
 
K

Ken Tucker [MVP]

Hi,

You need to set a size to the array before you use it. Try this
instead Private EmpArray(1) As Employees.Employee and redim it when you
change the size. You would be better off using an arraylist or collection
if you dont have a set size.

Ken
--------------------
I keep getting the error message: An unhandled exception of type
'System.NullReferenceException' occurred in Final2.exe

Additional information: Object reference not set to an instance of an
object.

I am new to programming and am not sure what is wrong. I feel like I have
tried everything. Any advice would be appreciated. Thanks.

This is the code in the class library:

Option Strict On
Option Explicit On

Imports System.IO
Imports System.Convert
Imports System.Windows.Forms

'Define the root namespace as Course. In the Course namespace,
'define another namespace named People. The People namespace, in turn,
'should have a class named Employees.
Namespace People
Public Class Employees

Public Structure Employee
Public ID As Integer
Public LastName As String
Public FirstName As String
Public Address As String
Public City As String
Public State As String
Public ZipCode As String
Public AnnualSales As Single
End Structure

'Declare a hidden array of Employee sructures so the developer can
retrieve
'records from the array by calling defined methods.
Public pEmployee() As Employee

' The FileOk event handler fires if the user clicks the OK button on the
OpenFileDialog.
' Attempt to open the file. Create a structured error handler to display
a message if
' the file cannot be opened for some reason.
Public Sub New(ByVal FileName As String)
Dim pstrLine As String
Dim pintCount As Integer
Dim pstrFields() As String
Dim pchrDelimiter As Char() = {CChar(",")}


Try
Dim srdCurrent As New StreamReader(FileName)
pstrLine = srdCurrent.ReadLine
Do Until pstrLine = Nothing
ReDim Preserve pEmployee(pintCount)

' Divide the current line into individual fields.
pstrFields = pstrLine.Split(pchrDelimiter)
With pEmployee(pintCount)
.ID = ToInt32(pstrFields(0))
.LastName = pstrFields(1)
.FirstName = pstrFields(2)
.Address = pstrFields(3)
.City = pstrFields(4)
.State = pstrFields(5)
.ZipCode = pstrFields(6)
.AnnualSales = ToSingle(pstrFields(7))
End With
pintCount += 1

pstrLine = srdCurrent.ReadLine
Loop
srdCurrent.Close()

Catch ex As System.Exception

MessageBox.Show("Can't Load File", "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub

End Class
End Namespace

This is the code in the Main Form:

Option Strict On
Option Explicit On

Imports System.IO
Imports System.Convert
Imports Course.People

Public Class Form1
Inherits System.Windows.Forms.Form

Private EmpClass As Employees
Private EmpArray() As Employees.Employee

Public intCurrentRecord As Integer

Private Sub mmuFileOpen_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mmuFileOpen.Click
ofdMain.ShowDialog()
'open the file by creating an instance of the class.
EmpClass = New Employees(ofdMain.FileName)
Call ShowCurrentRecord(intCurrentRecord)

End Sub

' Display the current record.
Friend Sub ShowCurrentRecord(ByVal pintCurrent As Integer)
EmpArray(pintCurrent) = New Employees.Employee ' I just added
this line to see if it would help with no luck.
txtIDNumber.Text = EmpArray(pintCurrent).ID.ToString
txtLastName.Text = EmpArray(pintCurrent).LastName
txtFirstName.Text = EmpArray(pintCurrent).FirstName
txtAddress.Text = EmpArray(pintCurrent).Address
txtCity.Text = EmpArray(pintCurrent).City
txtState.Text = EmpArray(pintCurrent).State
txtZipCode.Text = EmpArray(pintCurrent).ZipCode
txtAnnualSales.Text = EmpArray(pintCurrent).AnnualSales.ToString
End Sub
 
I

Imran Koradia

Try
Dim srdCurrent As New StreamReader(FileName)
pstrLine = srdCurrent.ReadLine
Do Until pstrLine = Nothing
ReDim Preserve pEmployee(pintCount)
You haven't initialized the pEmployee(pintCount) object which is why
when access the object in the code below (With pEmployee(pintCount)) you get
the error you mentioned. Just initialize each employee object as:
pEmployee(pintCount) = New Employee
before accessing the object. Then your code below should run through fine.

' Divide the current line into individual fields.
pstrFields = pstrLine.Split(pchrDelimiter)
With pEmployee(pintCount)
.ID = ToInt32(pstrFields(0))
.LastName = pstrFields(1)
.FirstName = pstrFields(2)
.Address = pstrFields(3)
.City = pstrFields(4)
.State = pstrFields(5)
.ZipCode = pstrFields(6)
.AnnualSales = ToSingle(pstrFields(7))
End With
pintCount += 1


hope that helps..
Imran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top