Working with someone else's VB.Net
code. A View
is throwing 'Object reference not set to an instance of an object.'
, which I understand is "The object has not been initialized before use.". Can't pinpoint where exactly the issue is even after debugging with breakpoints. I tried suggested ideas from here but not much luck.
Sharing the Functions involved, if anyone can spot the issue:
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
The View
code where it's breaking is:
<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>
Controller:
<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
The issue is on the Controller
function. When the view is called, it is passing an empty object to the view, which apparently is Nothing
for VB.Net
or Null
in c#
.
Here the object is created but not populated with data.
If m_Error = True Then
Dim m_EmailTemplate As New UXEmailTemplate <<< this will throw NullReferrence exception
Return View(m_EmailTemplate)
End If
Solution: Populate some data for the object.
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
I found a very good explanation for the error Object reference not set to an instance of an object..