InfoPath - NullReferenceException:Object reference not set to an instance of an object

Asked By Rahul Khanna on 20-Jul-14 05:58 AM

I am reading the term store metadata information from /_vti_bin/TaxonomyClientService.asmx?WSDL webservice to fill the infopath dropdown list.But I am getting the "Object reference not set to an instance of an object" error at the "newNode = nav.Clone();" line. Here is my code:

XPathNavigator mainNav = this.MainDataSource.CreateNavigator();
cleanupDropdown(mainNav, "/my:myFields/my:Countries");
XPathNavigator termSetNav = this.DataSources["GetCountries"].CreateNavigator();
string xPath = "/dfs:myFields/dfs:dataFields/tns:GetChildTermsInTermSetResponse/tns:GetChildTermsInTermSetResult";
XPathNavigator nodeNav = termSetNav.SelectSingleNode(xPath, this.NamespaceManager);
string resultXML = nodeNav.Value.ToString();
XmlDocument termSetXML = new XmlDocument();
termSetXML.LoadXml(resultXML);
XmlNodeList terms = termSetXML.GetElementsByTagName("T");
XPathNavigator nav = this.CreateNavigator().
                SelectSingleNode("/my:myFields/my:Countries", this.NamespaceManager);// Note: "/my:myFields/my:Countries" => Group Countries

foreach (XmlNode term in terms)
 {
 XmlNode v = term.SelectSingleNode("LS/TL");
 string termValue = v.Attributes["a32"].Value;
 string termGuid = term.Attributes["a9"].Value;

 XPathNavigator newNode = null;
 newNode = nav.Clone(); //Object reference not set to an instance of an object.
 newNode.SelectSingleNode("/my:myFields/my:Countries/my:Displayname",
                    this.NamespaceManager).SetValue(termValue);
                newNode.SelectSingleNode("/my:myFields/my:Countries/my:Value",
                this.NamespaceManager).SetValue(termValue);
                nav.InsertAfter(newNode);
                newNode = null;             

 }
 nav.DeleteSelf();
 nav = null;







Robbe Morris replied to Rahul Khanna on 21-Jul-14 07:25 AM
This line didn't return a node:

XPathNavigator nav = this.CreateNavigator(). SelectSingleNode("/my:myFields/my:Countries", this.NamespaceManager);//
Rahul Khanna replied to Robbe Morris on 22-Jul-14 12:04 PM
thanks for your reply. I have figured it out, the problem is in the below line:

cleanupDropdown(mainNav, "/my:myFields/my:Countries");

Comment out the above line it will work, because 
"/my:myFields/my:Countries" is valid node it should field xpath not group xpath, anyway I dont need to call the 
cleanupDropdown() method.