【问题标题】:Object reference not set to instance of an object in VB.NET对象引用未设置为 VB.NET 中的对象实例
【发布时间】:2014-02-15 04:52:01
【问题描述】:

为什么我的代码会出现错误“对象引用未设置为对象的实例”?

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() 是一个全局变量

谢谢

更新:

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

【问题讨论】:

  • 哪一行出错了?什么是名称?是数组吗?名称的代码在哪里?
  • 什么是名称?是数组吗?
  • 错误指向:Names(i) = txtPatientName.Text,Names 是一个数组,它被声明为模块中所有表单的全局变量
  • 我确定您无法访问此变量并因此出错?你能展示一下 Names 是如何声明的吗
  • 你需要将模块声明为Public Module Module1

标签: vb.net instance


【解决方案1】:

您需要将模块设为公开。所以我建议如下

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

然后以如下形式访问它

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

【讨论】:

  • 我仍然收到错误“'Module1' 是一种类型,不能用作表达式”
  • 这是正确的做法,但是当添加记录时,数组需要在事件上“redim preserve”。
【解决方案2】:

如果你坚持使用一个模块,你应该 redim 保留你的数组。

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

【讨论】:

  • 感谢按钮 2 的代码对我有用,但是“i”只会在第一次点击时增加一次,在输入另一个名称后,它不会增加
  • 好吧,我的尺寸是 form1。将 I 添加到您的模块中。 ` Public i As Integer = 0`
  • 老实说,来自同一个表单 (form1),它应该具有相同的工作方式,但要使其与表单兼容,无论如何您都需要这样做。
  • 我将它添加到我的模块中,但是它在达到 1 后仍然没有增加
  • 在增加 I 后将 MsgBox(i.ToString) 添加到 button2。它应该增加一个 msg。它在我的源代码中。
猜你喜欢
  • 2013-01-14
  • 2011-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 2013-12-30
  • 1970-01-01
相关资源
最近更新 更多