title: System.NullReferenceException: Object reference not set to an instance of an object. in vb.net
body: <p>this code on vb.net is giving me this error :</p>
  <blockquote>
  <p>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.</p>
  <p>Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.</p>
  </blockquote>
  <p>Any idea why and how to fix it?</p>
  <pre><code>     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(&quot;Family&quot;) = clsFamily
      If clsFamily Is Nothing Then
  
          clsFamily = New clsFamily
      Else
          clsFamily = Session(&quot;Family&quot;)
      End If
      Dim eror As Integer = 0
      Dim eror_message As String = &quot;Wrong input, please try again&quot;
      If TBxPN.Text Is &quot;&quot; Or TBxPN.Text Is eror_message Or Regex.IsMatch(TBxPN.Text, &quot;^[0-9 ]+$&quot;) Then
          TBxPN.Text = eror_message
          eror = 1
      End If
  
      If TBxOcc.Text Is &quot;&quot; Or TBxOcc.Text Is eror_message Or Regex.IsMatch(TBxOcc.Text, &quot;^[0-9 ]+$&quot;) 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(&quot;Family&quot;) = 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
  </code></pre>