如何修复 vb 中 dbcontext 的空引用错误?

How do I fix a null reference error on a dbcontext in vb?

我的代码构建并运行,但是当我在运行时引用 dbcontext 时,我得到 "System.NullReferenceException: 'Object reference not set to an instance of an object.'"。我在解决方案中的很多其他地方使用上下文并且它有效,但它是 c# 其他地方。这是 VB.

Imports DataServices
Imports Previdence.Domain.Model
Imports Previdence.Business.Model.Report

Namespace UserControls    
Partial Class RemissionControl
        Inherits UserControl
        Private previdenceContext As PrevidenceContext
        Private patient As Subject
        Private remissionButtonStatus As Boolean?
        Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

            Dim snapshotId As Guid = Utility.StringToGuid(Request.QueryString("snapshotId"))
            patient = (From su In previdenceContext.Subjects
                       Join ep In previdenceContext.Episodes On su.SubjectId Equals ep.SubjectId
                       Join sn In previdenceContext.Snapshots On ep.SubjectId Equals sn.SnapshotId
                       Where sn.SnapshotId = snapshotId
                       Select su).FirstOrDefault()
            remissionButtonStatus = patient.RemissionButtonOn
            If remissionButtonStatus = True Then
                remissionRButtonYes.Checked = True
                remissionRButtonNo.Checked = False
            Else
                remissionRButtonYes.Checked = False
                remissionRButtonNo.Checked = True
            End If
        End Sub

        'TODO: getting null reference error on the dbcontext
        Private Sub remissionRButtonYes_click() Handles remissionRButtonYes.CheckedChanged
            If remissionRButtonYes.Checked = True Then
                patient.RemissionButtonOn = True
            Else patient.RemissionButtonOn = False
            End If
            previdenceContext.SaveChanges()
        End Sub

        Private Sub remissionRButtonNo_click() Handles remissionRButtonNo.CheckedChanged
            If remissionRButtonNo.Checked = True Then
                patient.RemissionButtonOn = False
            Else patient.RemissionButtonOn = True
            End If
            previdenceContext.SaveChanges()
        End Sub
    End Class
End Namespace

这应该是 IntelliSense 捕获的错误类型。

无论如何,如果您替换声明,您将避免错误: Private previdenceContext As PrevidenceContextPrivate previdenceContext As New PrevidenceContext,但是您的代码仍然无法工作,因为 previdenceContext 没有数据。

您可能缺少一行代码来填充 previdenceContext

因为您在 class 的多个成员中使用了 previdenceContext,所以您可以在 class 级别声明它,并在 Sub New() 中实例化它。必须处理上下文,因此您的 class 需要实施 IDisposable 并处理 previdenceContext.

Partial Class RemissionControl
    Implements IDisposable
    Inherits UserControl

    Private previdenceContext As PrevidenceContext

    Sub New()
        ' This is the parameterless constructor. If you have a constructor with
        ' parameter such as passing the connection string name, use it. Do what you
        ' you do in your C# code.
        previdenceContext New PrevidenceContext()
    End Sub

    ' Your class implementation ...

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
                If previdenceContext IsNot Nothing Then previdenceContext.Dispose()
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
        Dispose(True)
        ' TODO: uncomment the following line if Finalize() is overridden above.
        ' GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class