Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Visual Basic 2005 Error NullReferenceException was unhandled

Posted on 2008-06-16
5
Medium Priority
?
435 Views
Last Modified: 2013-11-26
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("FindHalfProductsResponse")("Products")("Product")("ItemArray")
'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

Open in new window

Bookit-Error-Msg.jpg
0
Comment
Question by:bpfsr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:margajet24
ID: 21799413

// have you check if [request.GetResponse()] returns a value?
            Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)

 // if it does not, then strReader will be null
            Dim strReader As StreamReader = New StreamReader(response.GetResponseStream(), encoding)

 // then strReader.ReadToEnd() will not be valid because no refence was created
// resulting to NullReferenceException
            'Dim result As String = strReader.ReadToEnd()
0
 

Author Comment

by:bpfsr
ID: 21799420
How would I check that? I do know when I step through it does not stop there...
0
 

Author Comment

by:bpfsr
ID: 21799439
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?
0
 

Author Comment

by:bpfsr
ID: 21799481
If it helps here is the exception detail:

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="FindHalfProducts_HTTP_GET"
  StackTrace:
       at FindHalfProducts_HTTP_GET.Module1.Main()
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
0
 
LVL 10

Accepted Solution

by:
margajet24 earned 2000 total points
ID: 21799561
trace which variable is Nothing before it is used in another process

or just add try-catch then debug per line.. the last line that will be executed before the catch will help you determine the cause of the error

try
{
//codes
}
catch ( NullReferenceException e )
{
MessageBox.Show(e.Message);
}
0

Featured Post

Implementing Azure Infrastructure Exam 70-533

This course is designed to familiarize and instruct students in the content that is covered by Microsoft Exam 70-533, Implementing Microsoft Azure Solutions. It focuses on all the November 2016 objective domain topics.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question