Greetings. I'm having a rough time solving this problem.

this is my global dec

Dim btns As Button() = New Button() {Button1, Button2}

and this is my eventhandler

        Dim btnName As Button = DirectCast(sender, Button)
        Dim i As Integer
        btns(i) = New Button
        For i = 0 To btns.Length Step 1

            toothName = " " & btns(i).Text & " - Upper " <- in here it says Object reference not set to an instance of an object

        Next

can you guys please tell me the other possibilities why i'm getting this error? Thanks.

Recommended Answers

All 3 Replies

What exactly are you trying to do? You have an array of Button then you give a variable btnName a type Button without using it. Then you take your button in the array and try to create a new button without the array index. Not sure what event handler you are using either I suppose it's the click events for both buttons you have in the array.

Anyway I believe your problem is the array is not being loaded when the form is opened. i ran your code and added the buttons to the array in the load event. Not sure why your looping thru the array and setting the value of toothName it will always be your second button name

    Dim btns(1) As Button
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Try
            btns(0) = Me.Button1
            btns(1) = Me.Button2
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try


    End Sub

    Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
        'Dim btnName As Button = DirectCast(sender, Button)
        Dim i As Integer
        'btns(i) = New Button
        Dim toothName As String = ""

        Try
            For i = 0 To btns.Length - 1
                toothName = " " & btns(i).Text & " - Upper " '<- in here it says Object reference not set to an instance of an object
            Next
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try


    End Sub

That will stop your Object Reference Not Set Error and maybe you can go on from there

Sorry for not providing the full details. heres what i was trying to do.

I have 32 buttons (names are button1 ... button32) and a contextmenustrip. what i was trying to do is when i press right click on a button and click on a menustrip item, it will get the buttons text (e.g 1|8) and also the menustrips text.

so i was trying to make an array that will hold all of my 32 buttons and make them hold a mousedown handler.

Your loop [For i = 0 To btns.Length Step 1] is wrong. The array starts from base index 0 to (array.length - 1). So, while you loop to [i = btns.Length], then [btns(i)] will be NULL. It'll occur this error. So, you just modified your loop from 0 to [btns.Length - 1] (Ex: For i = 0 To btns.Length - 1 Step 1). This error has been fixed. Good luck!

PS: Sorry if my English is bad. Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.