gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:06:54 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com