bpfsr
asked on
Visual Basic 2005 Error NullReferenceException was unhandled
I am getting the above error message and can't find a solution. I have attached the message box that pops up and the code snippet where I am getting the error. Specifically when I step through I get the error at Dim root As XmlNode = xmlDoc("FindHalfProductsRe sponse")(" Products") ("Product" )("ItemArr ay")
'iterate through the records
Do While myReader.Read()
myReader(2).ToString()
'get the ISBN
'example to get a value from the data reader - myReader(0).ToString()
'you will need to replace it with the correct field value
ISBN = "val" 'obtained from myReader
'Server URL - with the ISBN read from the database
Dim serverURL As String = "http://open.api.ebay.com/shopping?callname=FindHalfProducts&responseencoding=XML&appid=[myid]&version=565&ProductID.type=ISBN&ProductID.Value=" + ISBN + "&IncludeSelector=Items"
'Create a new HttpWebRequest object
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(serverURL), HttpWebRequest)
'Get a HTTPWeb Response with HTTP GET
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
'Put the data into a UTF8 encoded byte array, since we deal with UTF8 characters
Dim encoding As New UTF8Encoding()
'Get the StreamReader
Dim strReader As StreamReader = New StreamReader(response.GetResponseStream(), encoding)
'This is the xml string - Alternately you can convert it to an XML object like XMLDoc and parse it
'Dim result As String = strReader.ReadToEnd()
Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(strReader.ReadToEnd())
strReader.Close()
Dim root As XmlNode = xmlDoc("FindHalfProductsResponse")("Products")("Product")("ItemArray")
For Each node As XmlNode In root.ChildNodes
Dim ItemID As String = node("ItemID").InnerText
Dim Seller As String = node("StoreName").InnerText
Dim Feedback As String = node("FeedbackScore").InnerText
Dim price As String = node("CurrentPrice").InnerText
Dim condition As String = node("HalfItemCondition").InnerText
Bookit-Error-Msg.jpg
ASKER
How would I check that? I do know when I step through it does not stop there...
ASKER
I think we are thinking along the same lines, I'm just not sure how to check the problem. I had thought the problem would be somewhere in this part:
'get the records from the database
Dim myReader As SqlDataReader = cmd.ExecuteReader()
'variable to store the ISBN from the database
Dim ISBN As String
'iterate through the records
Do While myReader.Read()
myReader(2).ToString()
Because I figured if it was getting the records correctly from the reader, it would be returning either no value or data that didn't make sense to it. If I read you correctly you are basically saying the same thing, no?
'get the records from the database
Dim myReader As SqlDataReader = cmd.ExecuteReader()
'variable to store the ISBN from the database
Dim ISBN As String
'iterate through the records
Do While myReader.Read()
myReader(2).ToString()
Because I figured if it was getting the records correctly from the reader, it would be returning either no value or data that didn't make sense to it. If I read you correctly you are basically saying the same thing, no?
ASKER
If it helps here is the exception detail:
System.NullReferenceExcept ion was unhandled
Message="Object reference not set to an instance of an object."
Source="FindHalfProducts_H TTP_GET"
StackTrace:
at FindHalfProducts_HTTP_GET. Module1.Ma in()
at System.AppDomain._nExecute Assembly(A ssembly assembly, String[] args)
at System.AppDomain.ExecuteAs sembly(Str ing assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.Hos tingProces s.HostProc .RunUsersA ssembly()
at System.Threading.ThreadHel per.Thread Start_Cont ext(Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
System.NullReferenceExcept
Message="Object reference not set to an instance of an object."
Source="FindHalfProducts_H
StackTrace:
at FindHalfProducts_HTTP_GET.
at System.AppDomain._nExecute
at System.AppDomain.ExecuteAs
at Microsoft.VisualStudio.Hos
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Threading.ThreadHel
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
// have you check if [request.GetResponse()] returns a value?
Dim response As HttpWebResponse = DirectCast(request.GetResp
// if it does not, then strReader will be null
Dim strReader As StreamReader = New StreamReader(response.GetR
// then strReader.ReadToEnd() will not be valid because no refence was created
// resulting to NullReferenceException
'Dim result As String = strReader.ReadToEnd()