Object reference not set to an instance of an object with API URL

When I place the API Url string on the browser, I get the data as expected but in the vb.net page, I get the following error: System.NullReferenceException: Object reference not set to an instance of an object

This is the extract from the page where the url is displayed. Any ideas how to fix this issue? I will also be using parmeterized query as well

Dim doc As New XmlDocument()
doc.Load("http://api.tradedoubler.com/1.0/products.xml;q=paris?token=A87185243749DAC9685310F6FEEB7F9038DD7F88")

Are you referencing the System.XML?

This runs and returns data on .Net Fiddle.

Imports System
Imports System.IO
Imports System.Xml
				
Public Module Module1

	Public Sub Main()
		Dim doc As New XmlDocument()
		doc.Load("http://api.tradedoubler.com/1.0/products.xml;q=paris?token=A87185243749DAC9685310F6FEEB7F9038DD7F88")
		Dim nodes As XmlNodeList = doc.DocumentElement.SelectNodes("/")
        For Each node As XmlNode In nodes
			Console.WriteLine(node.Name)
        Next		
	End Sub
End Module

test3.txt (4.1 KB)
yes i have. i m using vb.net. I have attached the file - renamed the extension from .aspx to txt.

I suspect the issue is from this line onward as the values may not be read from the file but not sure: Select Name = node.SelectSingleNode(“ns2:name”, nsManager).InnerText, _

Sorry I’m not being more helpful but I don’t typically do vb.net but I can tell you it’s probably this portion…

City = node.SelectSingleNode("ns2:fields/ns2:field[@name='city']", nsManager).InnerText, _
Country = node.SelectSingleNode("ns2:fields/ns2:field[@name='Country']", nsManager).InnerText, _

If I split the datasource line to write out the individual selects as writelines, all of them work except for the two looking for the city and country. https://dotnetfiddle.net/xP78e4

Imports System.Xml
				
Public Module Module1
	Public Sub Main()
		Dim doc As New XmlDocument()
		doc.Load("http://api.tradedoubler.com/1.0/products.xml?token=A87185243749DAC9685310F6FEEB7F9038DD7F88")

		Dim nsManager As New XmlNamespaceManager(doc.NameTable)
		nsManager.AddNamespace("ns1", "urn:com:tradedoubler:pf:model:xml:output")
		nsManager.AddNamespace("ns2", "urn:com:tradedoubler:pf:model:xml:common")
		Dim nodes As XmlNodeList = doc.SelectNodes("//ns1:products/ns1:product", nsManager)

		For Each node As XmlNode in nodes
			Console.WriteLine(node.SelectSingleNode("ns2:name", nsManager).InnerText)
			Console.WriteLine(node.SelectSingleNode("ns2:description", nsManager).InnerText)
			Console.WriteLine(node.SelectSingleNode("ns2:productImage", nsManager).InnerText)
			' Console.WriteLine(node.SelectSingleNode("ns2:fields/ns2:field[@name='City']", nsManager).InnerText)
			' Console.WriteLine(node.SelectSingleNode("ns2:fields/ns2:field[@name='Country']", nsManager).InnerText)
			Console.WriteLine(node.SelectSingleNode("ns1:offers/ns1:offer/ns1:priceHistory/ns2:price", nsManager).InnerText)
			Console.WriteLine(node.SelectSingleNode("ns1:offers/ns1:offer/ns2:productUrl", nsManager).InnerText)
			Console.WriteLine(node.SelectSingleNode("ns1:offers/ns1:offer/ns2:programName", nsManager).InnerText)
			Console.WriteLine(node.SelectSingleNode("ns1:offers/ns1:offer/ns2:programLogo", nsManager).InnerText)
		Next
		Console.WriteLine("Hello World")
	End Sub	
End Module

Thanks for pointing that out. i will check the API tree structure. Thanks again

1 Like

Quick question on this API. is it possible to connect to a second or even third API on the same page so for instance, I may use other providers and want to know if it is possible to add just add their link to the same page:

Dim doc As New XmlDocument()
doc.Load("http://api.tradedoubler.com/1.0/products.xml;q=paris?token=A87185243749DAC9685310F6FEEB7F9038DD7F88") 

You need to use try and catch with the DOM.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.