使用Properties时出现Null Reference Exception

时间:2011-02-15 16:22:59

标签: .net vb.net properties nullreferenceexception

你看到问题所在吗?代码将从frmFacility运行,并将转换为UserControl:

Public Class frmFacility
Private primaryBaseDay As Date
Private isClassPrimaryView As Boolean = False
Friend WithEvents BookCtrl As ucBookCtrl2
Public Property Primary_BaseDay() As Date
    Get
      Return primaryBaseDay
    End Get
    Set(ByVal value As Date)
      primaryBaseDay = value
    End Set
  End Property

  Public Property IsOnPrimaryView() As Boolean
    Get
      Return isClassPrimaryView
    End Get
    Set(ByVal value As Boolean)
      isClassPrimaryView = value
    End Set
  End Property
Public Sub GotoDay(ByVal theDay As Date)
    Primary_BaseDay = theDay
    IsOnPrimaryView = True
    BookCtrl.GotoDay(theDay)
End Sub
End Class

   Imports frmFacility
Public Class ucBookCtrl2
Public Sub GotoDay(ByVal whichDay As Date, Optional ByVal MainFacilityUsed As String = "")
Dim facilityForm As frmFacility
If facilityForm.IsOnPrimaryView Then
        moDoBooking.m_BaseDay = facilityForm.Primary_BaseDay
        moDoBooking.m_CurrentDay = whichDay
        ShowDay()
        RaiseEvent ChangeOfDay()
End Sub
End Class

If facilityForm.IsOnPrimaryView Then行,我得到NullReferenceException。你知道原因吗?

此外,我无法创建facilityForm的新实例,因为我需要使用其单例,但是当我添加frmFacility的新实例时,IsOnPrimaryMode已设置到falsetrue来自gotoday的{​​{1}}。{/ 1}}。{/ 1}

3 个答案:

答案 0 :(得分:3)

您没有将facilityForm设置为任何内容,您只需声明frmFacility类型的变量并将其命名为facilityForm。当您尝试拨打facilityForm.IsOnPrimaryView时,facilityForm仍为null

答案 1 :(得分:1)

您实际上并没有使用facilityForm单例。你必须把它存放在某个地方。也许是Public Class Shared test as New frmFacility

Form还没有IsOnPrimaryView属性,因为它没有实例化。实际上是Nothing

答案 2 :(得分:0)

您需要将IsOnPrimaryView属性设置为shared,否则您将获得NullReference异常。