-4

I am fetching data parallely from amazon site using ISBN number with vb.net code..

Its working some ISBN number only..

when we used some other ISBN number.It will display below error:

nullreferenceexception was unhandled. Object reference not set to an instance of an object.

How to solve this? Anyone help..

Here is the code:

Any error in this code? Anyone give solution..

Private Sub ButtonProductAdvertisingAPIItemLookup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonProductAdvertisingAPIItemLookup.Click sender.Enabled = False

    'Product Advertising API, ItemLookup: http://docs.aws.amazon.com/AWSECommerceService/2011-08-01/DG/ItemLookup.html

    Dim ItemId(9) As String

    ItemId(0) = System.Uri.EscapeDataString(TextBoxisbn1.Text)
    ItemId(1) = System.Uri.EscapeDataString(TextBoxisbn2.Text)
    ItemId(2) = System.Uri.EscapeDataString(TextBoxisbn3.Text)
    ItemId(3) = System.Uri.EscapeDataString(TextBoxisbn4.Text)
    ItemId(4) = System.Uri.EscapeDataString(TextBoxisbn5.Text)
    ItemId(5) = System.Uri.EscapeDataString(TextBoxisbn6.Text)
    ItemId(6) = System.Uri.EscapeDataString(TextBoxisbn7.Text)
    ItemId(7) = System.Uri.EscapeDataString(TextBoxisbn8.Text)
    ItemId(8) = System.Uri.EscapeDataString(TextBoxisbn9.Text)
    ItemId(9) = System.Uri.EscapeDataString(TextBoxisbn10.Text)

    Dim i As Integer = 0
    'For Each ItemId As String In ItemIds
    'Parallel.ForEach(ItemIds, Sub(ItemId)
    System.Threading.Tasks.Parallel.ForEach(ItemIds, Sub(ItemId)
                                                         'For Each ItemId As String In ItemIds()


                                                         'Next
                                                         ' Dim ItemId As String = System.IO.Path.GetFileName(currentFile)
                                                         My.Settings.ItemId = ItemId

                                                         Dim MyREST As New SprightlySoftAWS.REST

                                                         Dim RequestURL(i) As String
                                                         RequestURL(i) = "https://webservices.amazon.com/onca/xml?Service=AWSECommerceService&Operation=ItemLookup&Version=2011-08-01"
                                                         RequestURL(i) += "&SignatureVersion=2&SignatureMethod=HmacSHA256&Timestamp=" & Uri.EscapeDataString(DateTime.UtcNow.ToString("yyyy-MM-dd\THH:mm:ss.fff\Z"))
                                                         RequestURL(i) += "&AssociateTag="
                                                         RequestURL(i) += "&ResponseGroup=" & System.Uri.EscapeDataString("ItemAttributes,Large,OfferSummary,Offers,VariationSummary,Reviews")
                                                         RequestURL(i) += "&ItemId=" & System.Uri.EscapeDataString(ItemId)

                                                         'RequestURL(i) += "&ItemId=" & System.Uri.EscapeDataString(ItemId)

                                                         RequestURL(i) += "&IdType=ISBN"
                                                         RequestURL(i) += "&SearchIndex=Books"

                                                         Dim RequestMethod As String
                                                         RequestMethod = "GET"

                                                         Dim SignatureValue As String
                                                         SignatureValue = MyREST.GetSignatureVersion2Value(RequestURL(i), RequestMethod, "", "")

                                                         RequestURL(i) += "&Signature=" & System.Uri.EscapeDataString(SignatureValue)

                                                         Dim RetBool As Nullable(Of Boolean)
                                                         RetBool = MyREST.MakeRequest(RequestURL(i), RequestMethod, Nothing)

                                                         Debug.Print("")
                                                         Debug.Print(MyREST.LogData)
                                                         Debug.Print("")

                                                         Dim Y As Integer
                                                         Dim ResponseMessage As String = ""


                                                         If RetBool = True Then
                                                             Dim MyXmlDocument As System.Xml.XmlDocument
                                                             Dim MyXmlNamespaceManager As System.Xml.XmlNamespaceManager
                                                             Dim MyXmlNode As System.Xml.XmlNode
                                                             Dim MyXmlNodeList As System.Xml.XmlNodeList

                                                             MyXmlDocument = New System.Xml.XmlDocument
                                                             MyXmlDocument.LoadXml(MyREST.ResponseString)

                                                             MyXmlNamespaceManager = New System.Xml.XmlNamespaceManager(MyXmlDocument.NameTable)
                                                             MyXmlNamespaceManager.AddNamespace("amz", "http://webservices.amazon.com/AWSECommerceService/2011-08-01")

                                                             MyXmlNodeList = MyXmlDocument.SelectNodes("amz:ItemLookupResponse/amz:Items/amz:Item", MyXmlNamespaceManager)

                                                             If MyXmlNodeList.Count = 0 Then
                                                                 ResponseMessage = "Item not found."
                                                             Else

                                                                 For Each ItemXmlNode As System.Xml.XmlNode In MyXmlNodeList
                                                                     MyXmlNode = ItemXmlNode.SelectSingleNode("amz:ASIN", MyXmlNamespaceManager)
                                                                     ResponseMessage += "Details of " & MyXmlNode.InnerText

                                                                     'Dim vbCrLf As New String(ValueType)

                                                                     ResponseMessage += vbCrLf
                                                                     ' ResponseMessage += vbCrLf

                                                                     MyXmlNode = ItemXmlNode.SelectSingleNode("amz:ItemAttributes/amz:Title", MyXmlNamespaceManager)
                                                                     ResponseMessage += "Title = " & MyXmlNode.InnerText

                                                                     ResponseMessage += vbCrLf

                                                                     MyXmlNode = ItemXmlNode.SelectSingleNode("amz:MediumImage/amz:URL", MyXmlNamespaceManager)
                                                                     ResponseMessage += "Image = " & MyXmlNode.InnerText

                                                                     ResponseMessage += vbCrLf

                                                                     MyXmlNodeList = ItemXmlNode.SelectNodes("amz:ItemAttributes/amz:Author", MyXmlNamespaceManager)
                                                                     If IsNothing(MyXmlNodeList) = False Then
                                                                         ResponseMessage += "Author = "
                                                                         For Each MyXmlNode In MyXmlNodeList
                                                                             ResponseMessage += MyXmlNode.InnerText & ", "
                                                                         Next
                                                                     End If

                                                                     ResponseMessage += vbCrLf

                                                                     MyXmlNode = ItemXmlNode.SelectSingleNode("amz:ItemAttributes/amz:ListPrice/amz:FormattedPrice", MyXmlNamespaceManager)
                                                                     ResponseMessage += "ListPrice = "
                                                                     If IsNothing(MyXmlNode) = False Then
                                                                         ResponseMessage += MyXmlNode.InnerText
                                                                     End If

                                                                     ResponseMessage += vbCrLf

                                                                     MyXmlNode = ItemXmlNode.SelectSingleNode("amz:Offers/amz:Offer/amz:OfferAttributes/amz:Condition", MyXmlNamespaceManager)
                                                                     ResponseMessage += "Condition = " & MyXmlNode.InnerText

                                                                     ResponseMessage += vbCrLf

                                                                     MyXmlNode = ItemXmlNode.SelectSingleNode("amz:Offers/amz:TotalOffers", MyXmlNamespaceManager)
                                                                     ResponseMessage += "TotalOffers = " & MyXmlNode.InnerText

                                                                     ResponseMessage += vbCrLf

                                                                     MyXmlNode = ItemXmlNode.SelectSingleNode("amz:OfferSummary/amz:TotalNew", MyXmlNamespaceManager)
                                                                     ResponseMessage += "TotalNew = " & MyXmlNode.InnerText

                                                                     ResponseMessage += vbCrLf

                                                                     MyXmlNodeList = ItemXmlNode.SelectNodes("amz:OfferSummary/amz:LowestNewPrice/amz:FormattedPrice", MyXmlNamespaceManager)
                                                                     ResponseMessage += "LowestNewPrice = "
                                                                     Y = 0
                                                                     For Each MyXmlNode In MyXmlNodeList
                                                                         If Y < 25 Then
                                                                             ResponseMessage += "" & MyXmlNode.InnerText & "  "
                                                                         End If
                                                                     Next

                                                                     ResponseMessage += vbCrLf

                                                                     MyXmlNode = ItemXmlNode.SelectSingleNode("amz:OfferSummary/amz:TotalUsed", MyXmlNamespaceManager)
                                                                     ResponseMessage += "TotalUsed = " & MyXmlNode.InnerText

                                                                     ResponseMessage += vbCrLf

                                                                     MyXmlNode = ItemXmlNode.SelectSingleNode("amz:OfferSummary/amz:LowestUsedPrice/amz:FormattedPrice", MyXmlNamespaceManager)
                                                                     ResponseMessage += "LowestUsedPrice = "
                                                                     If IsNothing(MyXmlNode) = False Then
                                                                         ResponseMessage += "" & MyXmlNode.InnerText
                                                                     Else
                                                                         ResponseMessage += "0"
                                                                     End If

                                                                     ResponseMessage += vbCrLf

                                                                     'MyXmlNode = ItemXmlNode.SelectSingleNode("amz:SalesRank", MyXmlNamespaceManager)
                                                                     'ResponseMessage += "SalesRank = " & MyXmlNode.InnerText

                                                                     'ResponseMessage += vbCrLf
                                                                 Next
                                                             End If

                                                             DialogOutput.Text = "Success"
                                                             DialogOutput.TextBoxOutput.Text += vbCrLf & ResponseMessage & vbCrLf


                                                         Else
                                                             ResponseMessage = FormatLogData(MyREST.RequestURL, MyREST.RequestMethod, MyREST.RequestHeaders, MyREST.ResponseStatusCode, MyREST.ResponseStatusDescription, MyREST.ResponseHeaders, MyREST.ResponseStringFormatted, MyREST.ErrorNumber, MyREST.ErrorDescription)

                                                             DialogOutput.Text = "Error"
                                                             DialogOutput.TextBoxOutput.Text = ResponseMessage
                                                             ' DialogOutput.ShowDialog(Me)
                                                             ' DialogOutput.Dispose()
                                                         End If
                                                         i = i + 1
                                                     End Sub)
    'Next
    DialogOutput.ShowDialog(Me)
    ClearTextBox(Me)
    sender.Enabled = True
End Sub
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – Cody Gray Aug 02 '13 at 07:00
  • Refer above code .Tell me any error in this code?How to solve this error: "nullreferenceexception was unhandled. Object reference not set to an instance of an object"... – softwaresolution Aug 02 '13 at 10:39
  • Please refer the above code and give any solution for that problem..Still i got the error.... – softwaresolution Aug 03 '13 at 11:49

2 Answers2

0

You can use nullables to catch that error.

Dim isbn As Integer?

  • Hi Hypernate. Yes..ISBN is an integer.. I am used nullable value type..refer below line. Dim RetBool As Nullable(Of Boolean) How to catch the error? Give some code.. – softwaresolution Aug 02 '13 at 10:16
  • Well if you use the Nullable value (dim x as Integer? –  Aug 02 '13 at 11:38
0

Where is the yellow line comming if you debug the program?

  • The error shows whole part of the above code..there is no yellow line coming for specific line while debug the program..the full code covered by dotted lines... – softwaresolution Aug 03 '13 at 04:42
  • i am put the code inside the try, catch statement.when i debug the code,yellow line coming below lines. Debug.Print(MyREST.LogData)..when i remove this line..the whole part of the code shows that nullreferenceexception error ... – softwaresolution Aug 03 '13 at 07:30
  • Anyone help..still i didn't get solution..I got the same error(nullreferenceexception). – softwaresolution Aug 03 '13 at 11:47
  • Still i got that exception(Nullreferenceexception) error..any one help? – softwaresolution Aug 05 '13 at 11:13
  • Any one help?i got this nullreferenceException error for some ISBN numbers while running above code...some other ISBN working properly..What's the problem? – softwaresolution Aug 16 '13 at 12:00