Click here to Skip to main content
14,330,776 members
Rate this:
Please Sign up or sign in to vote.
When I place the API Url string on the browser, I get the data as expected but in the 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()

What I have tried:

The syntax of the url is correct since it works on the browser
Updated 7-Oct-19 8:03am
ZurdoDev 7-Oct-19 13:59pm
It means something in your code is null and you are trying to use it. Just debug it and figure it out.
hm9 7-Oct-19 14:02pm
Everything works in the code apart from the url i posted. It doesnt like the url for some reason even though it works on the browser!
MadMyche 7-Oct-19 15:00pm
Are any errors being thrown? You may want to use a try...catch block to see any error values.
Another option would be to go the long way; use an HTTP Client instance to retrieve the URL and get the string and pass it into the Load() method; it makes it a lot easier to solve problems and would be easier to modify if the API being utilized has some request parameter requirements you are not meeting
hm9 7-Oct-19 15:12pm
below is the code just give you context. The url is correcct but It doesnt appear to be getting the values from the xml but I am sure it worked before...

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" Debug="true" %>

<%@ import Namespace="System.Data" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Web.UI.WebControls" %>
<%@ import Namespace="System.Xml" %>
<%@ import Namespace="System.Net.HttpWebResponse" %>
<%@ import Namespace="System.Linq" %>
<%@ import Namespace="System.Configuration"%>
<%@ Import Namespace="System.Xml.XLinq"%>
<%@ Import Namespace="System.Xml.Linq"%>
<%@ Import Namespace="System.Xml.XPath"%>

Sub Button_Click( s As Object, e As EventArgs )
if IsValid then

end if
end sub

sub Page_Load
if Not Page.IsPostBack then

Dim doc As New XmlDocument()
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)

Dim dataSource As IEnumerable
dataSource = From node As XmlNode in nodes
Select Name = node.SelectSingleNode("ns2:name", nsManager).InnerText, _
Description = node.SelectSingleNode("ns2:description", nsManager).InnerText, _
Image = node.SelectSingleNode("ns2:productImage", nsManager).InnerText, _
Price = node.SelectSingleNode("ns1:offers/ns1:offer/ns1:priceHistory/ns2:price", nsManager).InnerText, _
ProductUrl = node.SelectSingleNode("ns1:offers/ns1:offer/ns2:productUrl", nsManager).InnerText, _
Logo = node.SelectSingleNode("ns1:offers/ns1:offer/ns2:programLogo", nsManager).InnerText
rpMyRepeater.DataSource = dataSource

End if
end sub

<asp:repeater id="rpMyRepeater" onitemcommand="Button_Click" runat="Server">










<asp:image runat="server" width="100" height="100"
="" imageurl="<%# Eval("Image") %>">
<%# Eval("Name") %>


£<%# Eval("Price") %> <asp:image runat="server" width="130" onclick="Button_Click" imageurl="<%# Eval("logo")%>">
" target="_blank" ">Go To Website

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values. And it's very unlikely for that code to be generating the exception, unless there is something very, very wrong with the data being returned.
So start with the debugger, and verify the exact line that generates the error. If it is that line, then you need to load the returned HTML into a string and have a close look at that. It may be as simple as there is nothing sensible returned because you aren't loading it into a browser that has a valid Agent string, or logged in user...

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!

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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100