无法使用 VB 应用在 ASP.Net MVC 上查明空引用异常

Can't pinpoint null reference exception on ASP.Net MVC with VB app

提问人:PineCone 提问时间:3/11/2021 最后编辑:PineCone 更新时间:3/18/2021 访问量:134

问:

使用别人的代码。A是抛出的,我的理解是“对象在使用前尚未初始化”。即使在使用断点进行调试后,也无法确定问题的确切位置。我尝试了从这里提出的想法,但运气不大。VB.NetView'Object reference not set to an instance of an object.'

如果有人能发现问题,请共享所涉及的功能:

Public Function EditBefore(id As String) As UXEmailTemplate
    Dim m_EmailTemplate As UXEmailTemplate = GetEmailTemplate(id)
        Try

            Dim m_GetEmailTemplate As New UXEmailTemplate

            With m_GetEmailTemplate
                .Versions = GetVersions(m_EmailTemplate.ParentID).ToList()
            End With

            With m_EmailTemplate
                .Versions = m_GetEmailTemplate.Versions
            End With

            Return m_EmailTemplate

        Catch ex As Exception
            ex.ToString()
            _c.WriteError(System.Reflection.MethodInfo.GetCurrentMethod.ToString, String.Concat("ERROR: ", ex.ToString))
            Return m_EmailTemplate
        End Try
    End Function


        Public Function GetEmailTemplate(id As String) As UXEmailTemplate

        Dim m_EmailTemplates As List(Of UXEmailTemplate)
        GetEmailTemplate = Nothing

        m_EmailTemplates = GetAllEmailTemplates()

        If m_EmailTemplates IsNot Nothing Then
            For Each m_EmailTemplate As UXEmailTemplate In m_EmailTemplates
                If m_EmailTemplate.ID.Equals(id) Then
                    GetEmailTemplate = m_EmailTemplate
                    Exit For
                End If
            Next
        Else
            Return Nothing
        End If

    End Function

它被破坏的代码是:View

  <div Class="col-sm-4">
      @If (Model.Versions.Count > 1) Then            <<<<<<< here exception occurs (returns Nothing)
        @<div Class="cardFull" style="padding-top:20px;">
            <div Class="labelUX">Email Template Versions</div>
        </div>
      @<div Class="cardFull Checkboxlisten">
           <div id="CheckBoxlisten" Class="CheckboxlistenContent" style="background-color: lightgrey;">
               @For Each item In Model.Versions
                @<p>Version <a href="\KI\NewsletterEdit\@item.ID">@item.Version</a></p>Next
                                </div>
      </div>End If
    </div>

控制器:

 <HttpPost()>
    <ValidateInput(False)>
    <ValidateAntiForgeryToken()>
    Function NewsletterEdit(<Bind(Include:="ID, SendFrom, Subject,Text, HtmlText,CreatedDate, Version, ParentID")> ByVal item As UXEmailTemplate, url As String) As ActionResult
        If ModelState.IsValid Then
            Dim m_Error As Boolean = False
            If item Is Nothing Then
                ModelState.AddModelError("", "unexpected error")
                m_Error = True
            End If

            Dim m_Message As String = String.Empty

            If Not m_Error Then
                m_Message = dbEmail.EditEmailTemplate(item)
            End If

            If Not String.IsNullOrEmpty(m_Message) Then
                ModelState.AddModelError("", m_Message)
                m_Error = True
            End If

            If m_Error = True Then
                Dim m_EmailTemplate As New UXEmailTemplate
                Return View(m_EmailTemplate)
            End If

            If String.IsNullOrEmpty(url) Then
                Return RedirectToAction("../KI/Newsletter")
            Else
                Return Redirect(url)
            End If
        Else
            Return View(User)
        End If

    End Function
vb.net nullreferenceexception 对象引用

评论

0赞 jmcilhinney 3/11/2021
堆栈跟踪告诉您什么?
0赞 Hursey 3/12/2021
好吧,不要以为这会直接导致错误,我正在查看第一个代码示例。创建 New UXEmailTemplate,然后使用 .版本 = GetVersions(m_EmailTemplate.ParentID)。ToList() 之前,你给它任何真正的价值。
0赞 PineCone 3/16/2021
堆栈跟踪@jmcilhinney说"System.NullReferenceException: 'Object reference not set to an instance of an object.' UXBestPractice.Models.UXEmailTemplate.Versions.get returned Nothing."

答:

0赞 PineCone 3/18/2021 #1

问题出在功能上。当调用视图时,它会将一个空对象传递给视图,该对象显然是 for 或 in 。ControllerNothingVB.NetNullc#

此处创建了对象,但未填充数据。

If m_Error = True Then
   Dim m_EmailTemplate As New UXEmailTemplate <<< this will throw NullReferrence exception
       Return View(m_EmailTemplate)
End If

解决方案:填充对象的一些数据。

If m_Error = True Then
   Dim m_EmailTemplate As New UXEmailTemplate 
   m_EmailTemplate = GetETemplate(id)   <<<<call the function which will return the object for the view 
       Return View(m_EmailTemplate)
End If

我找到了一个很好的解释,用于错误对象引用未设置为对象的实例。