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
--------------------------------
From: Jessica Sterner
-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)
<Id>fWj0lG6qb0OUc6W4SULdXA==</Id>