"Object reference not set to an instance of an object."

MyForumID

Active member
Joined
Sep 25, 2005
Messages
26
Programming Experience
Beginner
[Resolve] "Object reference not set to an instance of an object."

Hello,

So I've started trying to use more control object arrays thanks to the help I received here earlier and I've managed to figure out how to handle some events e.g. keypress, but for some reason other events in the "System.EventArgs" set e.g. "Leave" and "TextChanged" result in the above error whenever they are called.

I assign the handle with "address of" when filling the array:

VB.NET:
This Results in an error:
private sub FillTextboxArray()
 for row = 1 to 5 step 1
         Dim box As New TextBox
  AddHandler box.TextChanged, AddressOf ArrayHandler_TextChanged
 
  Me.MyGroup.Controls.Add(box)
  Me.MyArray(row) = box
 next row
end sub
Private Sub ArrayHandler_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ArrayHandler.TextChanged
        ArrayHandler.Text = "Hello"
End Sub
 
This does not:
private sub FillTextboxArray()
 for row = 1 to 5 step 1
         Dim box As New TextBox
  AddHandler box.KeyPress, AddressOf ArrayHandler_KeyPress
 
  Me.MyGroup.Controls.Add(box)
  Me.MyArray(row) = box
 next row
end sub
Private Sub ArrayHandler_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ArrayHandler.KeyPress
        ArrayHandler.Text = "Hello"
End Sub

It would really help clean my code up a great deal if I could handle the "Leave" and "TextChanged" events through the arrays as the only way I can get it to work is to make separate functions for each of the ton of boxes I need. I also have to copy the reference through a separately named variables and then use those separately variable names to handle these events.

Any help would be much appreciated.

Thanks as always in advance.
 
Last edited:
I'm not sure what the ArrayHandler object is as in 'Handles ArrayHandler.KeyPress' and ArrayHandler.Text = "Hello".

Here's a few pointers anyway.
Since the methods ArrayHandler_TextChanged and ArrayHandler_KeyPress have the same signatures and do the exact same thing, you could use one method that handles both events:
VB.NET:
Private Sub ArrayHandler_KeyPress_TextChanged(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.KeyPressEventArgs) _
  Handles ArrayHandler.KeyPress, _
  Handles ArrayHandler.TextChanged
        ArrayHandler.Text = "Hello"
End Sub
I realize this may be just sample code, this is just a pointer.

If you want to obtain a reference to the control that raised the event, use the first parameter 'sender' and type cast it to the correct type (using CType).
 
I stared at this and couldn't figure out what was wrong. It all looked OK to me. Then it hit me. You can't change the text in the TextChanged event... that would result in an infinite loop, because as soon as you change it, the change text would fire again, changin your text, causing the change even to fire, changing your text, causing the change text event to fire, changing..... well you get the idea.

That's the only thing I can think of.

-tg
 
actually that's not true

if you change the text in the textchanged event to "Hello" then yes the textchanged event would fire again but the text would still be "Hello" so the event wouldnt fire again

it's the same concept as the SelectedIndexChanged event for the Listbox/Combobox
 
Paszt said:
Since the methods ArrayHandler_TextChanged and ArrayHandler_KeyPress have the same signatures and do the exact same thing,
They do? They look different to me.


Private Sub ArrayHandler_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ArrayHandler.TextChanged

Private Sub ArrayHandler_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)


JuggaloBrotha said:
actually that's not true

if you change the text in the textchanged event to "Hello" then yes the textchanged event would fire again but the text would still be "Hello" so the event wouldnt fire again

it's the same concept as the SelectedIndexChanged event for the Listbox/Combobox
You are in fact quite right. I thought about that afterwards ....

-tg
 
Hello and thank you very much for the responses so far.

I made a mistake in my example and I shuldn't have had the statement:

ArrayHandler.Text = "Hello"

and just put a message box writing "Hello" since that really wasn't the main issue. Even with a message box that doesn't change the text it gives the error.

The "ArrayHandler" can be named anything - it's just a textbox variable that I'm using to be the main reference that all the elements of the array are linked to so that I can use it's functions. I thought when I posted it I included the dim statement but I forgot sorry - it's:

Friend WithEvents ArrayHandler As System.Windows.Forms.TextBox


The main difference I am seeing between the two is the arguments in the function:

System.EventArgs vs System.Windows.Forms.KeyPressEventArgs

But I don't understand nearly enough to know the significance.

As far as Keypress vs TextChanged - If I assign the text to the box from the program it's a change that will register only with TextChanged - correct? Keypress will be triggered only if the user types in the box - right? I use both and need the KeyPress for the validation while typing, but I need the textchanged because I use the text data for calculations that can be changed directly by the program and that effects other boxes.

I would also like to be able to use the "Leave" trigger which is a System.EventArgs for the final validation in the boxes which is based on values from other boxes and should only be checked when the typing is done.

Anyways, I'm sorry if I made things more confusing but any continued help is much appreciated.

Thanks.
 
Ok - I thought this was resolved and it's not - sorry again for the confusion.

It's capturing the .Leave and .KeyPress functions ok and allowing the message box to work.

When I try the following code, I get the error again:

VB.NET:
    Dim temp As String
    Dim MyTextArray(9)
    Friend WithEvents MyTextArrayHandler As System.Windows.Forms.TextBox
    Private Sub PopulateMyTextArray()
        Dim row As Integer
        
        For row = 0 To 9 Step 1
                Dim box As New TextBox
                        box.Location = New System.Drawing.Point(112, 80 + 24 * (row - 1))
                        box.Size = New System.Drawing.Size(40, 20)
 
                        'Add the TextBox to the array so it can be accessed by index.
                        Me.MyTextArray(column) = box
                        'Add the CheckBox to the Controls collection so it is visible.
                        Me.MyForm.Controls.Add(box)
                        'Add Handler to the general handler
                        AddHandler box.KeyPress, AddressOf MyTextArrayHandler_KeyPress
                        AddHandler box.Leave, AddressOf MyTextArrayHandler_Leave
        Next row
    End sub

    Private Sub MyTextArrayHandler_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyTextArrayHandler.KeyPress
        If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
            e.Handled = True
        End If
    End Sub
    Private Sub MyTextArrayHandler_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTextArrayHandler.Leave
        temp = MyTextArrayHandler.Text
    End Sub

So it seems it's the trying to access MyTextArrayHandler.Text that's causing the error.

Again any help appreciated.

Thanks
 
Last edited:
I went back and looked at my code for my checkbox array as well because it was working as I wanted and realized it never tried to address my "MyCheckboxArrayHandler.Checked" status which would be the equivalent of the "MyTextArrayHandler.Text" information and so I added a line to test it out and also got an error.

This time the error said:
System.NullReferenceException

Object reference not set to an instance of an object.

i.e. it's the same error. So it seems that the addhandler function is allowing me to access the functions of the boxes but not the actual data held by the boxes.

Is there a way to access the data in a generic fashion within a control array the way you can handle the functions?

Thanks in advance.
 
Ok so after only a million searches I finally stumbled upon the correct key words and found the following page on the MSFT site:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/controlarrays.asp

And it explained about the function:

DirectCast(object, ControlType)

That allows you to access the data within the generic object.

Below is code that works:

VB.NET:
[LEFT]Dim temp As String[/LEFT]
    Dim MyTextArray(9)
    Friend WithEvents MyTextArrayHandler As System.Windows.Forms.TextBox
    Private Sub PopulateMyTextArray()
        Dim row As Integer
        For row = 0 To 9 Step 1
                Dim box As New TextBox
                        box.Location = New System.Drawing.Point(112, 80 + 24 * (row - 1))
                        box.Size = New System.Drawing.Size(40, 20)
 
                        'Add the TextBox to the array so it can be accessed by index.
                        Me.MyTextArray(column) = box
                        'Add the CheckBox to the Controls collection so it is visible.
                        Me.MyForm.Controls.Add(box)
                        'Add Handler to the general handler
                        AddHandler box.KeyPress, AddressOf MyTextArrayHandler_KeyPress
                        AddHandler box.Leave, AddressOf MyTextArrayHandler_Leave
        Next row
    End sub
    Private Sub MyTextArrayHandler_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyTextArrayHandler.KeyPress
        'This function forces positive integer inputs in the textbox
        If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
            e.Handled = True
        End If
    End Sub
    Private Sub MyTextArrayHandler_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyTextArrayHandler.Leave
            'sender in this case refers to the object of the array that's being addressed
            temp = DirectCast(sender, TextBox).Text
    End Sub



I'm not sure I need the generic "arrayhandler" object but it's making my life easier for now and I don't know a way around it, but it's definitely something I can live with easily.


Thanks again everyone for you help - my code can finally move on :)
 
Back
Top