Results 1 to 14 of 14

Thread: How do I solve object reference not set to an instance of an object on line 9

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2013
    Posts
    43

    How do I solve object reference not set to an instance of an object on line 9

    In my function, on line 9 I am getting object reference not set to an instance of an object when I run the program. How do I solve it?

    VB Code
    Code:
    Function GetStatutesByStatuteId(ByVal aobjXmlGetStatuteRequestNode As XmlNode, ByVal aobjXMLNameSpaceManager As XmlNamespaceManager, ByVal aobjBroker As ServiceCatalog.Library.v4.Broker) As XmlDocument
        Dim objXmlRequestMessageDoc As XmlDocument
        Dim objXmlResponseMessageDoc As XmlDocument
    
        aobjBroker.PostMessageWarehouseInformationalMessage("StatuteId found.", 1)
    
        objXmlRequestMessageDoc = New XmlDocument
    	'I am getting object reference not set to an instance of an object on the line below. Dow do I solve it?
        objXmlRequestMessageDoc.AppendChild(objXmlRequestMessageDoc.CreateElement("ns:SingleStatuteRequest", aobjXMLNameSpaceManager.LookupNamespace("ns")))
        objXmlRequestMessageDoc.SelectSingleNode("ns:SingleStatuteRequest", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns:statuteId", aobjXMLNameSpaceManager.LookupNamespace("ns")))
    
        'Get the statutes
        'Set a document for the statutes
        objXmlResponseMessageDoc = New XmlDocument
        objXmlResponseMessageDoc.Load("\\j00000swebint\mscapps\deve\appfiles\temp\BcaStatutes.xml")
        Return objXmlResponseMessageDoc
    End Function
    Last edited by winkimjr2; Mar 10th, 2015 at 11:55 AM. Reason: I changed the objXmlStatutesDoc to objXmlRequestMessageDoc on both lines 9 and 10

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: How do I solve object reference not set to an instance of an object on line 9

    That means one of the objects in the chain of objects is nothing
    objXmlRequestMessageDoc.AppendChild(objXmlStatutesDoc.CreateElement("ns:SingleStatuteRequest", aobjXMLNameSpaceManager.LookupNamespace("ns")))

    since just on the line above you created objXmlRequestMessageDoc, odds are, that's not likely to be the issue.

    that leaves:
    objXmlStatutesDoc.CreateElement("ns:SingleStatuteRequest", aobjXMLNameSpaceManager.LookupNamespace("ns"))

    aobjXMLNameSpaceManager is passed in... a candidate, but probably not likely.... that leaves just one object:
    objXmlStatutesDoc - I don't see it passed in or created...



    on another note - what is objXmlRequestMessageDoc? you create it, set a few elements and then throw it away, loading a file into objXmlResponseMessageDoc and returning that... so what's the point of the request message doc object?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: How do I solve object reference not set to an instance of an object on line 9

    Whenever you get that exception, the first step is always the same: Find the object that is Nothing. In some rare cases, that can be difficult, but it won't be with this case. Examine each object in turn to find the one that is Nothing. Here are the candidates you should look at:

    1)objXmlRequestMessageDoc: This one pretty much can't be Nothing, because you created it in the previous line, but it is still worth looking at if you are studying the problem.

    2) objXmlStatutesDoc: This is a likely candidate, since I don't see where it comes from.

    3) aobjXMLNameSpaceManager: This is also a likely candidate.

    One of those three is Nothing. Almost certainly it is the second or the third, with the second one being most likely. Once you determine which one is Nothing, the next step is to determine why it is Nothing. If it is either of those two, we can't say anything about how to fix it, because one comes from an unknown place and the other is an argument to the method. Where they come from isn't shown.

    EDIT: Not only was I too slow, I didn't add any extra value, either. Bummer.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2013
    Posts
    43

    Re: How do I solve object reference not set to an instance of an object on line 9

    I was able to fix the null reference. Now when I run my code I am not getting a value fir the StatuteId. aobjXmlGetStatuteRequestNode is the object where the statuteId is. I am not sure how to use the aobjXmlGetStatuteRequestNode to get the value on line 10

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: How do I solve object reference not set to an instance of an object on line 9

    Maybe what you need to do is backup and re-read my final note in post #2... I don't think that code does what you think it does... if you take a step back, and then describe what that sub is supposed to be doing -- NOT what the code does, but what it is SUPPOSED to do (the function exists for a reason, right?) -- then maybe we can begin to help. But the more I look at the code, the more wrong it looks to me.
    I still don't have an idea what the objXmlRequestMessageDoc is supposed to be or what it's for.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2013
    Posts
    43

    Re: How do I solve object reference not set to an instance of an object on line 9

    The objXmlRequestMessageDoc is an object for a new document. The function is called from a if statement and when there is a statuteId found in the original xml doc, then the function is called to create a new xml doc with single statuteId. The previous xml doc could have more than one statuteId but the function will only return a single one. The output should look like this
    Code:
    <ns:SingleStatuteRequest xmlns:ns="http://crimnet.state.mn.us/mnjustice/statute/service/4.0">
      <ns:statuteId>
          <ns:Id>15499</ns:Id>
       </ns:StatueId> 
      </ns:SingleStatuteRequest>
    My output is showing like this:
    Code:
     
     <ns:SingleStatuteRequest> 
          <ns:statuteId />  
     </ns:SingleStatuteRequest>
    [/CODE]

    Here is the original xml doc used by the if statement
    Code:
    <ns:GetStatutesRequest>
    	<ns:Statute>
    		<ns:StatueId>
    			<ns:Id>15499</ns:Id>
    			<ns:Id>34567</ns:Id>-->
    		</ns:StatueId>
    	</ns:Statute>
    </ns:GetStatutesRequest>
    Here is the if statement that calls the function
    Code:
    If Not objXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:StatueId/ss:Id[string-length(.)>0]", objXMLNameSpaceManager) Is Nothing Then
    
        objXmlBcaResponseDoc = GetStatutesByStatuteId(objXmlGetStatuteRequestNode, objXMLNameSpaceManager, aobjBroker)
    Else
         aobjBroker.Reply(aobjBroker.CreateSoapFault(Msc.Integration.Utility.Library.v4.Soap.udtSoapCodes.Sender, "StatuteId or Chapter is required.", Msc.Integration.Utility.Library.v4.Soap.udtSoapRoles.RoleUltimateReceiver, aobjXMLInputSoapEnvelopeDoc, "soap:InvalidMessage", "soap:Body", Msc.Integration.Utility.Library.v4.Soap.GetReplyEndpointReference(aobjXMLInputSoapEnvelopeDoc), aobjXMLInputSoapEnvelopeDoc.DocumentElement.SelectSingleNode("soap:Header/wsa:MessageID", objXMLNameSpaceManager).InnerText, aobjConsumer))
         Exit Sub
    End If

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: How do I solve object reference not set to an instance of an object on line 9

    Ok, that's not making much sense, either. In the first snippet, you create objXmlRequestMessageDoc, do something with it, then throw it away. It appears to have no net effect at all. That's what TG was getting at. This part of that function:

    Code:
     objXmlRequestMessageDoc = New XmlDocument
    	
        objXmlRequestMessageDoc.AppendChild(objXmlRequestMessageDoc.CreateElement("ns:SingleStatuteRequest", aobjXMLNameSpaceManager.LookupNamespace("ns")))
        objXmlRequestMessageDoc.SelectSingleNode("ns:SingleStatuteRequest", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns:statuteId", aobjXMLNameSpaceManager.LookupNamespace("ns")))
    Looks like it could be removed without changing the behavior of the program in any way. Some code acts via side effects, but there is nothing in that code that looks likely to have a side effect. Furthermore, if you take that code out of the method, then you don't need the first two arguments. So, why did you write that code? What did you expect it to accomplish?
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2013
    Posts
    43

    Re: How do I solve object reference not set to an instance of an object on line 9

    Quote Originally Posted by Shaggy Hiker View Post
    Ok, that's not making much sense, either. In the first snippet, you create objXmlRequestMessageDoc, do something with it, then throw it away. It appears to have no net effect at all. That's what TG was getting at. This part of that function:

    Code:
     objXmlRequestMessageDoc = New XmlDocument
    	
        objXmlRequestMessageDoc.AppendChild(objXmlRequestMessageDoc.CreateElement("ns:SingleStatuteRequest", aobjXMLNameSpaceManager.LookupNamespace("ns")))
        objXmlRequestMessageDoc.SelectSingleNode("ns:SingleStatuteRequest", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns:statuteId", aobjXMLNameSpaceManager.LookupNamespace("ns")))
    Looks like it could be removed without changing the behavior of the program in any way. Some code acts via side effects, but there is nothing in that code that looks likely to have a side effect. Furthermore, if you take that code out of the method, then you don't need the first two arguments. So, why did you write that code? What did you expect it to accomplish?
    What I want to do is create a new xml document that is based off the original document but this time only show one statuteId. The new xml documenet created by the function should look like this:
    Code:
    <ns:SingleStatuteRequest>
    	<ns:statuteId>15499</ns:statuteId>
    </ns:SingleStatuteRequest>
    By the way running my function is creating a new xml document by without a value for the statuteId.
    I was able to generate a new xml document and manually in the code added ns:statuteId 123.

    <ns:SingleStatuteRequest xmlns:ns="http://crimnet.state.mn.us/mnjustice/statute/service/4.0"><ns:statuteId>123</ns:statuteId></ns:SingleStatuteRequest>

    However I want to get this value from the object aobjXmlGetStatuteRequestNode because this object has the following in the outer xml
    Code:
    <ns:GetStatutesRequest xmlns:ns="http://www.courts.state.mn.us/StatuteService/1.0">
     <ns:Statute>
      <ns:StatueId>
        <ns:Id>15499</ns:Id> 
      </ns:StatueId>
     </ns:Statute>
    </ns:GetStatutesRequest>
    So based on this xml code in the object aobjXmlGetStatuteRequestNode I would like to change the following line of code to instead of displaying
    <ns:SingleStatuteRequest xmlns:ns="http://crimnet.state.mn.us/mnjustice/statute/service/4.0"><ns:statuteId>123</ns:statuteId></ns:SingleStatuteRequest>
    Code:
    
    
    I want to display the value 15499 and not 123.

    Here is the line of code in the function I want to change.
    Code:
    objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns:statuteId", aobjXMLNameSpaceManager).InnerText = "123"
    When I use the following code
    Code:
    objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns:statuteId", aobjXMLNameSpaceManager).InnerText = (aobjXmlGetStatuteRequestNode.SelectSingleNode("ns:SingleStatuteRequest/ns:statuteId", aobjXMLNameSpaceManager.LookupNamespace("ns")
    I get an error
    Value of type 'string' cannot be converted to 'System.Xml.XMLNameSpaceManager'
    Last edited by winkimjr2; Mar 10th, 2015 at 03:13 PM.

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: How do I solve object reference not set to an instance of an object on line 9

    What you say you want and what you're actually doing are not the same thing... and that's the problem...

    Compare this: (that presumably works)
    vb.net Code:
    1. objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns:statuteId", aobjXMLNameSpaceManager).InnerText = "123"
    with this:
    vb.net Code:
    1. objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns:statuteId", aobjXMLNameSpaceManager).InnerText = (aobjXmlGetStatuteRequestNode.SelectSingleNode("ns:SingleStatuteRequest/ns:statuteId", aobjXMLNameSpaceManager.LookupNamespace("ns")

    Why is it that when some people want to change something simple, they end up changing way more than they need to or should?

    Actually you only need to look at the one line to see the problem...
    vb.net Code:
    1. objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns:statuteId", aobjXMLNameSpaceManager).InnerText = (aobjXmlGetStatuteRequestNode.SelectSingleNode("ns:SingleStatuteRequest/ns:statuteId", aobjXMLNameSpaceManager.LookupNamespace("ns")
    In the first SelectSingleNode, you gave it the element to look for and the namespace manager... but in the second one, you gave it the element to look for an the namespace manager's lookup namespace... which returns a string... why? All you need to pass in is JUST the namespace manager... nothing more.

    you don't want this:
    aobjXmlGetStatuteRequestNode.SelectSingleNode("ns:SingleStatuteRequest/ns:statuteId", aobjXMLNameSpaceManager.LookupNamespace("ns")
    you want this
    aobjXmlGetStatuteRequestNode.SelectSingleNode("ns:SingleStatuteRequest/ns:statuteId", aobjXMLNameSpaceManager)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2013
    Posts
    43

    Re: How do I solve object reference not set to an instance of an object on line 9

    -tg thank you so much for taking time to help me. This is frustrating. Okay the first (SelectSingleNode) is appending child node <ns:statuteId /> It does not add a value to the <ns:statuteId />
    Code:
     objXmlRequestMessageDoc.SelectSingleNode("ns:SingleStatuteRequest", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns:statuteId", aobjXMLNameSpaceManager.LookupNamespace("ns")))
    is producing the following output.
    Code:
    <ns:SingleStatuteRequest xmlns:ns="http://crimnet.state.mn.us/mnjustice/statute/service/4.0">
      <ns:statuteId /> 
    </ns:SingleStatuteRequest>
    The second SelectSingleNode or what I want to do with the second SelectSingleNode is to add a value to the <ns:statuteId />. So the second Statement should produce the following output
    Code:
    <ns:SingleStatuteRequest xmlns:ns="http://crimnet.state.mn.us/mnjustice/statute/service/4.0">
      <ns:statuteId>15499</ns:statuteId>
    </ns:SingleStatuteRequest>
    This value is found inside the object aobjXmlGetStatuteRequestNode this is why I am using it like this
    Code:
    objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns:statuteId", aobjXMLNameSpaceManager).InnerText = (aobjXmlGetStatuteRequestNode.SelectSingleNode("ns:SingleStatuteRequest/ns:statuteId", aobjXMLNameSpaceManager))
    . But at this point the code break and I get Error Value of type 'System.Xml.XmlNode' cannot be converted to 'String' . So the value 15499 for the ns:statuteId is not added.
    Again thanks for helping and I hope this clears the air as to what I am trying to do.

    I just want my output to look like this
    Code:
    <ns:SingleStatuteRequest xmlns:ns="http://crimnet.state.mn.us/mnjustice/statute/service/4.0">
      <ns:statuteId>15499</ns:statuteId> 
    </ns:SingleStatuteRequest>
    where the value 15499 is found inside the object aobjXmlGetStatuteRequestNode. I want to read it from the object and add it to the new xml document as shown above.

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: How do I solve object reference not set to an instance of an object on line 9

    "But at this point the code break and I get Error Value of type 'System.Xml.XmlNode' cannot be converted to 'String' . So the value 15499 for the ns:statuteId is not added." -- right... because selectSingleNode returns a Node, just like it's name implies. Again... look at the left side of the = ... see how it selects a node? see how it then access the innerText? so....samething on the RIGHT side... select the node, then return the InnerText...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Member
    Join Date
    Sep 2013
    Posts
    43

    Re: How do I solve object reference not set to an instance of an object on line 9

    Quote Originally Posted by techgnome View Post
    "But at this point the code break and I get Error Value of type 'System.Xml.XmlNode' cannot be converted to 'String' . So the value 15499 for the ns:statuteId is not added." -- right... because selectSingleNode returns a Node, just like it's name implies. Again... look at the left side of the = ... see how it selects a node? see how it then access the innerText? so....samething on the RIGHT side... select the node, then return the InnerText...

    -tg
    I agree with you. However the changed line of code look like this
    Code:
    objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns:statuteId", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ns:GetStatutesRequest/ns:StatueId/ns:Id", aobjXMLNameSpaceManager).InnerText
    It is generating an error Object reference not set to an instance of an object. The aobjXmlGetStatuteRequestNode at this point has a value 15499.
    Here is the outer xml in the object and I have tried changing the xpath to ns:StatueId/ns:Id", and even to ns:Id but still getting nullreference regardless of the xpath
    outer xml in the aobjXmlGetStatuteRequestNode
    Code:
    <ns:GetStatutesRequest xmlns:ns="http://www.courts.state.mn.us/StatuteService/1.0">
    	<ns:Statute>
    		<ns:StatueId>
    			<ns:Id>15499</ns:Id>
    		</ns:StatueId>
    	</ns:Statute>
    </ns:GetStatutesRequest>
    I did use the correct namespace ss: instead of ns: This fixed the issue and now I am done at this point. There are other parts and when I am stuck I will post a question/s.
    Thank you again -tg for your help.
    Last edited by winkimjr2; Mar 11th, 2015 at 09:34 AM. Reason: The problem was caused by namespace

  13. #13
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: How do I solve object reference not set to an instance of an object on line 9

    drop the ns: from the strings.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    Member
    Join Date
    Sep 2013
    Posts
    43

    Re: How do I solve object reference not set to an instance of an object on line 9

    I did lol. Thanks

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