Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to get output from xml document and everything is working. However when I comment (remove SecurityGroup Word element), for that element I am getting Object reference not set to an instance of an object.
My xml document
XML
<pre lang="xml"><pre><Integration>
   <Case>
	<CaseCategory>FAM</CaseCategory>
	<CaseType Word="DMA">Domestic Abuse</CaseType>
	<BaseCaseType>Civil Domestic Violence</BaseCaseType>
	<CaseTitle>X v/s Y</CaseTitle>
	<!--<SecurityGroup Word="CONFPOR">Conf - Protective Order</SecurityGroup>-->
	<Court>
	   <NodeID>131</NodeID>
	</Court>
   </Case>
</Integration>


My vb.net code
VB
Dim strCaseTypeCodeWord As String
Dim strBaseCaseType As String
Dim strCaseSecurityGroupCodeWord As String
Dim intNodeID As Integer

strCaseTypeCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/CaseType/@Word").InnerText
strBaseCaseType = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/BaseCaseType").InnerText
strCaseSecurityGroupCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Case/SecurityGroup/@Word").InnerText
intNodeID = CInt(aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Assignment/Court/NodeID").InnerText)


Result
strCaseTypeCodeWord = DMA
strBaseCaseType = Civil Domestic Violence
strCaseSecurityGroupCodeWord This is where I am getting the Object reference not set to an instance of an object. because I commented/removed this element in xml document. It is an optional element.
What do I need to do so that the vb.net code will work even when this SecurityGroup does not exist in the xml document?

What I have tried:

VB
Dim strCaseTypeCodeWord As String
            Dim strBaseCaseType As String
            Dim strCaseSecurityGroupCodeWord As String
            Dim intNodeID As Integer
 strCaseTypeCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/CaseType/@Word").InnerText
                strBaseCaseType = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/BaseCaseType").InnerText
                strCaseSecurityGroupCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Case/SecurityGroup/@Word").InnerText
                intNodeID = CInt(aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Assignment/Court/NodeID").InnerText)
Posted
Updated 3-Feb-17 3:15am
Comments
[no name] 1-Feb-17 14:44pm    
Okay, this error happens for the exact same reason as everyone in the universe and you fix it the exact same way as everyone else. A simple google search would have told you.

On of the variable you're calling a method or getting/setting a property value on is null.

We can't tell you which because we don't have the data your code is using.

YOU can easily find out what's causing this just by using the debugger. When the code stops, the debugger will show you which line through the exception. Hover the mouse over each variable and object to inspect it's contents. At least one of them is going to be null. Then you have to trace your code backwards until you find why/how that variable/object got the value null.

Hover over each variable name, like strCaseTypeCodeWord, and object, like each part of aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/CaseType/@Word").
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 2-Feb-17 0:26am    
5
Quote:
How do I fix object reference not set to an instance of an object error?

If you expect
VB
aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Case/SecurityGroup/@Word").InnerText

to fail because the node is optional, you need to check if the variable holding the result is still a string or NULL. if the variable is NULL, set a default value.

VB
strCaseSecurityGroupCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Case/SecurityGroup/@Word").InnerText
if strCaseSecurityGroupCodeWord = NULL then
    ...
end if
 
Share this answer
 
Another option is to check if the Node is null before access it value.

C#
strCaseSecurityGroupCodeWord = aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Case/SecurityGroup/@Word") != null? aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/Case/SecurityGroup/@Word").InnerText : string.empty
 
Share this answer
 
This is how I resolved it also based on the solutions I got from Bryian Tan. If the node is not there I will set strCaseSecurityGroupCodeWord to empty string ""

VB
If aobjXMLInputDoc.DocumentElement.SelectSingleNode("Case/SecurityGroup/@Word") Is Nothing Then
                    strCaseSecurityGroupCodeWord = ""
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900