Results 1 to 3 of 3

Thread: How do I fix Object reference not set to an instance of an object?

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2013
    Posts
    43

    How do I fix Object reference not set to an instance of an object?

    I get CompletedDate from XML document element name CompDate. This is an optional element. When the XML document has a CompDate element my VB.NET code works. However when the XML document does not have CompDate element, I get Object reference not set to an instance of an object?

    How do I fix this so it works whether or not the CompDate element exist in the XML Document?

    XML Document vb code is reading


    Code:
    <Integration>
       <Case>
         <CaseEvent ID="252949395">
    	<CompDate>06/01/2019</CompDate>
         </CaseEvent>
       </Case>
       <IntegrationConditions>
          <IntegrationCondition>
    	 <NotificationEvent elementKey="252949395">InsertPWBRorAOS</NotificationEvent>
          </IntegrationCondition>
       </IntegrationConditions>
    </Integration>

    What I have tried:

    VB.NET code

    Code:
    Dim strEventId As String
    
    strEventId = aobjxmlNotificationEventNode.SelectSingleNode("@elementKey").InnerText
    
    objInsertPWBRorAOS.CompletedDate = CDate(aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/CaseEvent[@ID=" + strEventId + "]/CompDate").InnerText)

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: How do I fix Object reference not set to an instance of an object?

    Thread moved from the 'CodeBank VB.Net' forum (which is for you to post working code examples, not questions) to the 'VB.Net' forum


    I think this might solve it for you:
    Code:
    Dim strEventId As String
    strEventId = aobjxmlNotificationEventNode.SelectSingleNode("@elementKey").InnerText
    
    Dim CompDate = aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/CaseEvent[@ID=" + strEventId + "]/CompDate")
    If CompDate IsNot Nothing Then
      objInsertPWBRorAOS.CompletedDate = CDate(CompDate.InnerText)
    End If

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How do I fix Object reference not set to an instance of an object?

    VB also has null propagation these days, so you can say "get this member if the object exists, otherwise get Nothing":
    vb.net Code:
    1. Dim strEventId As String
    2. strEventId = aobjxmlNotificationEventNode.SelectSingleNode("@elementKey").InnerText
    3.  
    4. Dim CompDate = aobjXmlInputDoc.DocumentElement.SelectSingleNode("Case/CaseEvent[@ID=" + strEventId + "]/CompDate")?.InnerText
    5. If CompDate IsNot Nothing Then
    6.   objInsertPWBRorAOS.CompletedDate = CDate(CompDate)
    7. End If
    This:
    vb.net Code:
    1. x = y?.z
    is functionally equivalent to this:
    vb.net Code:
    1. If y Is Nothing Then
    2.     x = Nothing
    3. Else
    4.     x = y.z
    5. End If
    One of the cool things is that, because you can use the null propagation operator anywhere you would use a standard member access operator, you can chain them together, e.g.
    vb.net Code:
    1. a = b?.c?.d?.e
    That is functionally equivalent to this:
    vb.net Code:
    1. If b Is Nothing OrElse b.c Is Nothing OrElse b.c.d Is Nothing Then
    2.     a = Nothing
    3. Else
    4.     a = b.c.d.e
    5. End If
    Note that null propagation only works for reference types or nullable value types.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width