Click here to Skip to main content
15,891,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is wrong in this linq query, it is showing error? "Object reference not set to an instance of an object."
C#
String  StoreNumber="1259";

XElement xel = xDoc.Element("Items")
                           .Elements("Item")
                           .Where(x => x.Elements("StoreNumber").SingleOrDefault().Value == StoreNumber.Trim())
                           .SingleOrDefault();

XDocumnet is
XML
<Items>
   <Item>
    <ItemID>426</ItemID>
    <Name>1064</Name>
    <ParentID>307</ParentID>
    <DisplayName><![CDATA[1064]]></DisplayName>
    <ChildViewLayout>Store</ChildViewLayout>
    <Keywords><![CDATA[1064;]]></Keywords>
    <StoreNumber>1064</StoreNumber>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Entrance.txt;1000-Right.txt;1064-R.txt</PathFile>
  </Item>
  <Item>
    <ItemID>427</ItemID>
    <Name>1066</Name>
    <ParentID>307</ParentID>
    <DisplayName><![CDATA[1066]]></DisplayName>
    <ChildViewLayout>Store</ChildViewLayout>
    <Keywords><![CDATA[1066;]]></Keywords>
    <StoreNumber>1066</StoreNumber>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Entrance.txt;1000-Right.txt;1066-R.txt</PathFile>
  </Item>
  <Item>
    <ItemID>428</ItemID>
    <Name>1259</Name>
    <ParentID>309</ParentID>
    <DisplayName><![CDATA[1259]]></DisplayName>
    <ChildViewLayout>Store</ChildViewLayout>
    <Keywords><![CDATA[1259;]]></Keywords>
    <StoreNumber>1259</StoreNumber>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Entrance.txt;1101-Right.txt;1259-L.txt</PathFile>
  </Item>
  <Item>
    <ItemID>429</ItemID>
    <Name>1271</Name>
    <ParentID>309</ParentID>
    <DisplayName><![CDATA[1271]]></DisplayName>
    <ChildViewLayout>Store</ChildViewLayout>
    <Keywords><![CDATA[1271;]]></Keywords>
    <StoreNumber>1271</StoreNumber>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Entrance.txt;1101-Right.txt;1271-L.txt</PathFile>
  </Item>
  <Item>
    <ItemID>430</ItemID>
    <Name>Fire-Exit</Name>
    <ParentID>4</ParentID>
    <DisplayName><![CDATA[Fire Exit]]></DisplayName>
    <ItemImage>utils-fireexit.png</ItemImage>
    <ChildViewLayout>Utilities</ChildViewLayout>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Fire-Exit.txt</PathFile>
  </Item>
  <Item>
    <ItemID>431</ItemID>
    <Name>Rest-Room</Name>
    <ParentID>4</ParentID>
    <DisplayName><![CDATA[Rest Room]]></DisplayName>
    <ItemImage>utils-restroom.png</ItemImage>
    <ChildViewLayout>Utilities</ChildViewLayout>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Rest-Room.txt</PathFile>
  </Item>
  <Item>
    <ItemID>432</ItemID>
    <Name>Disabled-Rest-Room</Name>
    <ParentID>4</ParentID>
    <DisplayName><![CDATA[Disabled Rest Room]]></DisplayName>
    <ItemImage>utils-handicap.png</ItemImage>
    <ChildViewLayout>Utilities</ChildViewLayout>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Handicap-Rest-Room.txt</PathFile>
  </Item>
  <Item>
    <ItemID>433</ItemID>
    <Name>Management Office</Name>
    <ParentID>4</ParentID>
    <DisplayName><![CDATA[Management Office]]></DisplayName>
    <ChildViewLayout>Utilities</ChildViewLayout>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Management-Office.txt</PathFile>
  </Item>
  <Item>
    <ItemID>434</ItemID>
    <Name>Conference Hall</Name>
    <ParentID>4</ParentID>
    <DisplayName><![CDATA[Conference Hall]]></DisplayName>
    <ChildViewLayout>Utilities</ChildViewLayout>
    <StoreFloor>AAHOA</StoreFloor>
    <PathFile>Conference-Hall.txt</PathFile>
  </Item>
  <Item>
    <ItemID>435</ItemID>
    <Name>Offers</Name>
    <ParentID>0</ParentID>
    <DisplayName><![CDATA[Offers]]></DisplayName>
    <ItemImage>Offers.png</ItemImage>
    <ChildViewLayout>Button</ChildViewLayout>
  </Item>
  <Item>
    <ItemID>436</ItemID>
    <Name>For members</Name>
    <ParentID>435</ParentID>
    <DisplayName><![CDATA[For members]]></DisplayName>
    <ChildViewLayout>Offers</ChildViewLayout>
    <Tag>PromotionDataMembers</Tag>
  </Item>
  <Item>
    <ItemID>437</ItemID>
    <Name>For non members</Name>
    <ParentID>435</ParentID>
    <DisplayName><![CDATA[For non members]]></DisplayName>
    <ChildViewLayout>Offers</ChildViewLayout>
    <Tag>PromotionDataNonMembers</Tag>
  </Item>
</Items>
Posted
Updated 20-Feb-14 20:45pm
v3
Comments
Kornfeld Eliyahu Peter 21-Feb-14 2:40am    
What xDoc is? Where you initiate it?
AVNISHmishra 21-Feb-14 2:43am    
i have given xDoc below the question
AVNISHmishra 21-Feb-14 2:45am    
XDocument xDoc = here i set xDocument that given in question
AVNISHmishra 21-Feb-14 2:51am    
if i use

.Where(x => x.Element("StoreNumber").Value == StoreNumber.Trim()

istead of
.Where(x => x.Elements("StoreNumber").SingleOrDefault().Value == StoreNumber.Trim()

then its also not working, same error

because element keyword search only first element

AVNISHmishra 21-Feb-14 2:53am    
if i use

.Where(x => x.Element("ItemID").Value == itemID.Trim()

then its working fine, but i want search based on storeNumber

The problem is that not all your elements has StoreNumber node!
 
Share this answer
 
Comments
AVNISHmishra 21-Feb-14 4:16am    
Yes but what will i do to check !=null
AVNISHmishra 21-Feb-14 4:23am    
Thank you brother, this is the answer

XElement xel = xDoc.Element("Items")
.Elements("Item")
.Where(x => x.Element("StoreNumber") !=null)
.Where(x => x.Element("StoreNumber").Value == StoreNumber.Trim())
.SingleOrDefault();
This works for me with your xml content. Sry it's in VB.NET

VB
Dim stringContent = "<items><item><itemid>426</itemid><name>1064</name><parentid>307</parentid><displayname><![CDATA[1064</displayname><childviewlayout>Store</childviewlayout><keywords><![CDATA[1064;</keywords><storenumber>1064</storenumber><storefloor>AAHOA</storefloor><pathfile>Entrance.txt;1000-Right.txt;1064-R.txt</pathfile></item><item><itemid>427</itemid><name>1066</name><parentid>307</parentid><displayname><![CDATA[1066</displayname><childviewlayout>Store</childviewlayout><keywords><![CDATA[1066;</keywords><storenumber>1066</storenumber><storefloor>AAHOA</storefloor><pathfile>Entrance.txt;1000-Right.txt;1066-R.txt</pathfile></item><item><itemid>428</itemid><name>1259</name><parentid>309</parentid><displayname><![CDATA[1259</displayname><childviewlayout>Store</childviewlayout><keywords><![CDATA[1259;</keywords><storenumber>1259</storenumber><storefloor>AAHOA</storefloor><pathfile>Entrance.txt;1101-Right.txt;1259-L.txt</pathfile></item><item><itemid>429</itemid><name>1271</name><parentid>309</parentid><displayname><![CDATA[1271</displayname><childviewlayout>Store</childviewlayout><keywords><![CDATA[1271;</keywords><storenumber>1271</storenumber><storefloor>AAHOA</storefloor><pathfile>Entrance.txt;1101-Right.txt;1271-L.txt</pathfile></item><item><itemid>430</itemid><name>Fire-Exit</name><parentid>4</parentid><displayname><![CDATA[FireExit</displayname><itemimage>utils-fireexit.png</itemimage><childviewlayout>Utilities</childviewlayout><storefloor>AAHOA</storefloor><pathfile>Fire-Exit.txt</pathfile></item><item><itemid>431</itemid><name>Rest-Room</name><parentid>4</parentid><displayname><![CDATA[RestRoom</displayname><itemimage>utils-restroom.png</itemimage><childviewlayout>Utilities</childviewlayout><storefloor>AAHOA</storefloor><pathfile>Rest-Room.txt</pathfile></item><item><itemid>432</itemid><name>Disabled-Rest-Room</name><parentid>4</parentid><displayname><![CDATA[DisabledRestRoom</displayname><itemimage>utils-handicap.png</itemimage><childviewlayout>Utilities</childviewlayout><storefloor>AAHOA</storefloor><pathfile>Handicap-Rest-Room.txt</pathfile></item><item><itemid>433</itemid><name>ManagementOffice</name><parentid>4</parentid><displayname><![CDATA[ManagementOffice</displayname><childviewlayout>Utilities</childviewlayout><storefloor>AAHOA</storefloor><pathfile>Management-Office.txt</pathfile></item><item><itemid>434</itemid><name>ConferenceHall</name><parentid>4</parentid><displayname><![CDATA[ConferenceHall</displayname><childviewlayout>Utilities</childviewlayout><storefloor>AAHOA</storefloor><pathfile>Conference-Hall.txt</pathfile></item><item><itemid>435</itemid><name>Offers</name><parentid>0</parentid><displayname><![CDATA[Offers</displayname><itemimage>Offers.png</itemimage><childviewlayout>Button</childviewlayout></item><item><itemid>436</itemid><name>Formembers</name><parentid>435</parentid><displayname><![CDATA[Formembers</displayname><childviewlayout>Offers</childviewlayout><tag>PromotionDataMembers</tag></item><item><itemid>437</itemid><name>Fornonmembers</name><parentid>435</parentid><displayname><![CDATA[Fornonmembers</displayname><childviewlayout>Offers</childviewlayout><tag>PromotionDataNonMembers</tag></item></items>"
Dim storeNumber = "1259"

Dim xDoc = XDocument.Parse(stringContent)

Dim someItems = From x in xDoc.Descendants("Item") Where x.Element("StoreNumber") = storeNumber
For Each item in someItems
    Console.WriteLine(item.ToString)
Next

Dim singleItem = xDoc.Descendants("Item").Single(Function(x) x.Element("StoreNumber") = storeNumber)
Console.WriteLine(singleItem.ToString)
 
Share this answer
 
v2
Hi I m sure the problem is SingleOrDefault()! use FirstOrDefault() instead.

WHen using SingleOrDefault, you have to be 100% sure that each element is unique, if not an exception occure. in the other hand FirstOrDefault() returns the first element if there is duplicates.

Hope it helps
 
Share this answer
 

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



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