0

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
3
  • What does the stack trace tell you? Commented Mar 11, 2021 at 13:29
  • Well don't think this will be directly causing the error, I'm looking at that first code sample. You create the New UXEmailTemplate, then your using .Versions = GetVersions(m_EmailTemplate.ParentID).ToList() before it appears your giving it any real value. Commented Mar 11, 2021 at 19:54
  • @jmcilhinney the stack trace says "System.NullReferenceException: 'Object reference not set to an instance of an object.' UXBestPractice.Models.UXEmailTemplate.Versions.get returned Nothing." Commented Mar 16, 2021 at 9:01

1 Answer 1

0

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..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.