1

Why am I getting the error "Object reference not set to instance of an object" with my code?

Public Class Form2
  Dim i As Integer = 0

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click
        Me.Close()
    End Sub

  Private Sub btnEnterPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterPatient.Click

        Names(i) = txtPatientName.Text
        i = i + 1
  End Sub
End Class

Names() is a global variable

Thanks

Updated:

Module Module1
    Public Names() As String
    Public Heights() As Integer
    Public Weights() As Integer
End Module


Public Class Form2

    Dim i As Integer = 0

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click
        Me.Close()
    End Sub

    Private Sub btnEnterPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterPatient.Click


        ReDim Preserve Names(0 To i)
        Names(i) = txtPatientName.Text

        ReDim Preserve Heights(0 To i)
        Heights(i) = txtPatientHeight.Text

        ReDim Preserve Weights(0 To i)
        Weights(i) = txtPatientWeight.Text

        i = i + 1

    End Sub
End Class
17
  • Which line you are getting error? What is Names? Is it array? Where is code for Names? Commented Jan 24, 2014 at 9:08
  • What is Names? Is it array? Commented Jan 24, 2014 at 9:09
  • The error is pointing to: Names(i) = txtPatientName.Text, Names is an array and it is declared as a global variable across all forms in the module Commented Jan 24, 2014 at 9:10
  • 1
    I am sure you are not able to access this variable and hence getting error? can you show how Names is declared Commented Jan 24, 2014 at 9:11
  • 1
    you need to declare module as Public Module Module1 Commented Jan 24, 2014 at 9:40

2 Answers 2

1

If you insist on using a module, you should redim preserve your array.

Public Module Module1
    Public i As Integer = 0
    Public Names() As String
    Public Heights() As Integer
    Public Weights() As Integer
End Module

Public Class Form1
    Dim i As Integer = 0

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub btnEnterPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


        ReDim Preserve Names(0 To i)
        Names(i) = txtpatientName.Text

        ReDim Preserve Heights(0 To i)
        Heights(i) = txtpatientheight.Text

        ReDim Preserve Weights(0 To i)
        Weights(i) = txtpatientweight.Text

        i = i + 1

    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        For Each j In Names
            MsgBox(j.ToString)
        Next

    End Sub
End Class
Sign up to request clarification or add additional context in comments.

14 Comments

thankyou the code for button 2 worked for me, however 'i' will only increment once on the first click and after entering another name, it does not increment
well I is dimensioned in form1. add I into your module. ` Public i As Integer = 0`
honestly from the same form (form1) it should have worked the same but to make it compatible across forms you would need to anyway.
I added it into my module however it still does not increase after it reaches 1
add MsgBox(i.ToString) to button2 after you increment I. it should msg an increment. it does in my source.
|
0

You need to make module as public. So i suggest below

Public Module Module1
   Public Names() As String
   Public Heights() As Integer
   Public Weights() As Integer
End Module

Then access it in form like

Dim mod1 = Module1
mod1.Names(i) = txtPatientName.Text

2 Comments

I am still receiving the error "'Module1' is a type and cannot be used as an expression"
This was the correct course of action, but the array needed to be "redim preserve" on the event when a record is added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.