无法使用 VB 应用在 ASP.Net MVC 上查明空引用异常
Can't pinpoint null reference exception on ASP.Net MVC with VB app
使用其他人的 VB.Net
代码。 A View
抛出 'Object reference not set to an instance of an object.'
,我的理解是“对象在使用前尚未初始化”。即使在使用断点调试后也无法确定问题的确切位置。我尝试了 here 的建议,但运气不佳。
分享所涉及的功能,如果有人能发现问题:
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
问题出在 Controller
函数上。调用视图时,它会将一个空对象传递给视图,显然是 Nothing
for VB.Net
或 Null
in c#
.
此处创建了对象但未填充数据。
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
我找到了一个很好的错误解释 Object reference not set to an instance of an object.。
使用其他人的 VB.Net
代码。 A View
抛出 'Object reference not set to an instance of an object.'
,我的理解是“对象在使用前尚未初始化”。即使在使用断点调试后也无法确定问题的确切位置。我尝试了 here 的建议,但运气不佳。
分享所涉及的功能,如果有人能发现问题:
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
问题出在 Controller
函数上。调用视图时,它会将一个空对象传递给视图,显然是 Nothing
for VB.Net
或 Null
in c#
.
此处创建了对象但未填充数据。
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
我找到了一个很好的错误解释 Object reference not set to an instance of an object.。