asp.net - System.NullReferenceException : Object reference not set to an instance of an object. in vb.net

标签 asp.net vb.net compiler-errors

vb.net上的这段代码给我这个错误:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.


知道为什么以及如何解决它吗?
     Public Class Family
Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
End Sub
Protected Sub saveButton_Click(sender As Object, e As EventArgs) Handles saveButton.Click
    Dim clsFamily As New clsFamily
    Session("Family") = clsFamily
    If clsFamily Is Nothing Then

        clsFamily = New clsFamily
    Else
        clsFamily = Session("Family")
    End If
    Dim eror As Integer = 0
    Dim eror_message As String = "Wrong input, please try again"
    If TBxPN.Text Is "" Or TBxPN.Text Is eror_message Or Regex.IsMatch(TBxPN.Text, "^[0-9 ]+$") Then
        TBxPN.Text = eror_message
        eror = 1
    End If

    If TBxOcc.Text Is "" Or TBxOcc.Text Is eror_message Or Regex.IsMatch(TBxOcc.Text, "^[0-9 ]+$") Then
        TBxOcc.Text = eror_message
        eror = 1
    End If
    If eror = 0 Then
        For i = 0 To clsFamily.PersonName.Length

            Dim newName As String
            Dim newOccupation As String
            Dim newDate As Date
            Dim newGender As String
            Dim newRelation As String

            newName = TBxPN.Text
            newOccupation = TBxOcc.Text
            newDate = Calendar1.SelectedDate.ToShortDateString()
            newGender = RBLGender.SelectedItem.Text
            newRelation = CBLRelation.SelectedItem.Text
            ReDim Preserve clsFamily.PersonName(i)
            clsFamily.PersonName(i) = newName
            ReDim Preserve clsFamily.Occupation(i)
            clsFamily.Occupation(i) = newOccupation
            ReDim Preserve clsFamily.DOB(i)
            clsFamily.DOB(i) = newDate
            ReDim Preserve clsFamily.Gender(i)
            clsFamily.Gender(i) = newGender
            ReDim Preserve clsFamily.RelationType(i)
            clsFamily.RelationType(i) = newRelation

        Next i
        Session("Family") = clsFamily


    End If


End Sub
Public Class clsFamily
    Public PersonName() As String
    Public RelationType() As String
    Public DOB() As Date
    Public Gender() As String
    Public Occupation() As String

End Class

最佳答案

您的类中确实需要一个构造函数。下一个示例向personName数组添加一项:“”。这意味着该数组不再为空,因此没有NullReferenceException。

Public Class clsFamily
    Public PersonName() As String
    Public RelationType() As String
    Public DOB() As Date
    Public Gender() As String
    Public Occupation() As String


    Public Sub New()
        PersonName = New String(0) {""}
    End Sub

End Class

关于asp.net - System.NullReferenceException : Object reference not set to an instance of an object. in vb.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31963921/

相关文章:

vb.net - 如何从另一个数组字符串中随机获取数组?

c++ - V8编译器错误

c++ - 使用模板实现排序。如何使用比较器?

c# - 重定向后验证用户

c# - 循环遍历目录

c# - Application ["Key"] 和 Application.Contents ["Key"] 有什么区别?

xcode - 默认情况下,对于iOS4项目的XCode中已弃用的代码是否存在编译警告或错误?

asp.net - 如何根据 UICulture 设置进行 URL 路由?

asp.net-mvc - 用于从 FQDN 中提取二级域名的正则表达式?

asp.net - Webmethod 引发 "Cannot refer to an instance member of a class from within a shared method or shared member..."