Structures question (vb.net)

Karzack

New member
Joined
Mar 7, 2004
Messages
2
Location
Nebraska
Hello, Im new to programming in general. Did a little on apple IIEs but that wasnt much. I have desiced to try to learn again with vb.net and ran into something. Im learning with the "Visual Basic .NET Step by Step" book.

When I try to add a value to a variable in a structure I get an error.

An unhandled exception of type System.NullReferenceException
Additional information: Object reference not set to an instance of an object.

Example: Trying to make a small address book.
This structure and the sub are in the module.
frmAdd is the form that information is added to the address book.
Code:
   Structure Person
        Public firstName As String
        Public middleName As String
        Public lastName As String
        Public homeNumber As String
        Public email As String
        Public address As String
        Public city As String
        Public state As String
        Public workNumber As String
        Public cellNumber As String
        Public photo As Image
    End Structure

    Public page() As Person         Array for number of people in book
    Public pg As Short              value to determine how many pages

    Sub pageFill()
        page(pg).firstName = frmAdd.txtFirstName.Text              < error here
        page(pg).middleName = frmAdd.txtMiddleName.Text
        page(pg).lastName = frmAdd.txtLastName.Text
        page(pg).homeNumber = frmAdd.txtHomeNumber.Text
   end sub

Didnt put in the rest of the sub because it was the same as the first few lines.
Anyway. When I type in the values in the text boxes and hit the button that calles this sub. I get the error described above. At the first line of the sub.

I think I need to back off coding for now, I have a headache. :(
I have done some tread searching but didnt come up with anything.
 
It doesnt appear as if you have told the program what pg is or how many items the array will hold. Is there any other part of this code, such as a ReDim statement, or where you are setting the variable pg?

If not, before you are able to get to the array, you will need to ReDim it to let it hold a certain number of elements, and then you will need to define pg with a value to access the particular part of the array.
 
*sighs*

I just forgot to redim the array. :o Thank you.
Value for pg was set before sub was called.

*muddles under his breath*
 
Back
Top