473,386 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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>
Nov 21 '05 #1
2 1500
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
--------------------
"Jessica Sterner via .NET 247" <an*******@dotnet247.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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>
Nov 21 '05 #2
> 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.
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Chong | last post by:
I have an (exe) executable program created in VB.NET 2003 that calls to a MFC DLL written in VC++.NET 2003. I always get an error msg "NullReferenceException: Object Reference Not Set to an...
1
by: Chris Magoun | last post by:
I suddenly received an unexpected error in my project. I have been working on this project for some time without this issue. Nothing has changed in the form that caused the exception. A little...
6
by: blash | last post by:
Can someone help me? I really don't have a clue. My company staff told me they often got such error: "Object reference not set to an instance of an object." when they are in search result page...
3
by: Web Team | last post by:
Server Error in '/netapps' Application. -------------------------------------------------------------------------------- Object reference not set to an instance of an object. Description: An...
18
by: Microsoft | last post by:
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object" Dim g As System.Drawing.Graphics g.DrawString("Test", New Font("Arial", 12,...
7
by: Brett | last post by:
I'm not sure why I keep getting this error, "Object reference not set to an instance of an object". Private Function somefunction() as string Dim MyCurrentClass As New Class1 Try For i As...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
4
by: rushikesh.joshi | last post by:
Hi All, I have created my own WebControl and want to add it in my aspx page at runtime. it's compiling perfectly, but when i m going to execute, it gives me error of "Object reference not set...
1
by: Nathan Sokalski | last post by:
I have a UserControl that I declare programmatically as follows: Dim userctrl as New rightside_portal() The codebehind file for this UserControl looks like the following: Partial Public...
2
by: arun1985 | last post by:
In the project i am using i am having the following code and when i upload it to the server.Its givig me the following error in the global.cs file. Server Error in '/' Application. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.