0

this code on vb.net is giving me this error :

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.

Any idea why and how to fix it?

     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
Community
  • 1
  • 1

1 Answers1

-1

You do need a constructor in your class. Next example adds 1 item to the personName array: "". This means the array is no longer empty so there is no 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
R-C
  • 16
  • 1
  • To use this method to avoid the NullReferenceException in the OP's code, all the arrays will have to be initialised. – Blackwood Aug 12 '15 at 12:59