0

Well, the eBay SDK has a pretty nice selection of examples, but they are outdated, most will not work and you get a NullReferenceException on the line. I was a Windows coder for about 5 years (5 years ago at that, and only know enough .Net to get me by. I develop mostly large scale web-based applications)

This particular application polls the eBay API via a Windows Service at given intervals and updates an SQL database with pending orders to be shipped. This is not necessary as this code is straight forward and not the problem.

Here is the line of older VB .Net code in question, keep in mind intelliSense shows the code as valid in code view.

 Dim Transactions As TransactionTypeCollection 
 Transactions = apiCall.GetSellerTransactions("1/1/2012 5:51:09 AM", "1/30/2012 5:51:09 AM")

When this second line of code is ran I receive this error:

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

Visual Studio provides some troubleshooting tips such as making sure the object being set is not NULL (Nothing) before calling, and using the New keyword to create a new instance of the object before calling the method. I tried all combinations of these methods for instance:

Dim Transactions As New Transaction TypeCollection

or after Transaction was defined,

Transactions = New apicall.getSellerTransaction()
    'didnt think this would work but I've tried everything

These did not help, and also the first did not generate any additional errors (the second, as I assumed lets you know that getSellerTransaction() is not a constructor).

Any Suggestions?

Thanks for reading the long post, just wanted to be as thorough as possible. BTW I am using the latest eBay .NET SDK from developer.ebay.com trying to do a getSellerTransaction. I had simliar problems when generating tokens with but that fix was different. I think it is a syntax error. Thanks for any help. I will be here to answer any questions if you need more detail.

-Mike

Additional Code

I am using a simple streamwriter to capture just enough data from the transactions so that I know they work (when i get past this bug, the pending orders will be populated into an sql datasouce). This is also a Windows Service (hence theServiceWorkerThread) Also, the .Net demo applications provided in the eBay SDK (atleast for GetSellerTransactions fails with the same error code, same place)

 Private Sub ServiceWorkerThread(ByVal state As Object)
    ' Periodically check if the service is stopping.
    Do While Not Me.stopping
        ' Perform main service function here...
        Dim apiCall As GetSellerTransactionsCall = New GetSellerTransactionsCall(apiContext)

        Dim transactions As New TransactionTypeCollection

        'the line below causes the exception
        transactions = apiCall.GetSellerTransactions("1/1/2012 5:51:09 AM", "1/30/2012 5:51:09 AM")
        Dim trans As New TransactionType
        For Each trans In transactions

            Me.sysLog.WriteEntry("ItemId: " & trans.Item.ItemID)
            Me.sysLog.WriteEntry("TransId: " & trans.TransactionID)
            Me.sysLog.WriteEntry("TransPrice: " & trans.TransactionPrice.Value.ToString())
            Me.sysLog.WriteEntry("AmtPaid: " & trans.AmountPaid.Value.ToString())
            Me.sysLog.WriteEntry("qtyPurchased: " & trans.QuantityPurchased.ToString())
            Me.sysLog.WriteEntry("buyUserId; " & trans.Buyer.UserID)

        Next trans

        Thread.Sleep(60000)  ' Simulate some lengthy operations.
    Loop

    ' Signal the stopped event.
    Me.stoppedEvent.Set()
End Sub

<summary>
    Populate eBay SDK ApiContext instance with data from application configuration file
</summary>
<returns>ApiContext instance</returns>
<remarks></remarks>

 Private Function GetApiContext() As ApiContext

    'apiContext is a singleton
    'to  avoid duplicate configuration reading
    If (apiContext IsNot Nothing) Then
        Return apiContext
    Else
        apiContext = New ApiContext

        'set Api Server Url
        apiContext.SoapApiServerUrl = AppSettings("SopApiServerUrl")

        'declare new ApiCredential
        Dim apiCredential As ApiCredential = New ApiCredential
        'set Applcation settings (not needed with a User Token)
        apiCredential.ApiAccount.Application = AppSettings("AppId")
        apiCredential.ApiAccount.Certificate = AppSettings("AppCert")
        apiCredential.ApiAccount.Developer = AppSettings("DevId")

        'set our User Token
        apiCredential.eBayToken = AppSettings("UserToken")

        apiContext.ApiCredential = apiCredential

        'set eBay Site target to US
        apiContext.Site = SiteCodeType.US

        Return apiContext

    End If

End Function
Cray
  • 2,774
  • 7
  • 22
  • 32
Mike L.
  • 1,936
  • 6
  • 21
  • 37

1 Answers1

3

The problem is not Transactions being Nothing but apiCall being Nothing.

Make sure that apiCall is initialized to a proper value.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Dim apiCall As GetSellerTransactionsCall = New GetSellerTransactionsCall(apiContext) – Mike L. Feb 01 '12 at 12:58
  • This has a value, and it is correct. This same code is used somewhere else that works fine. – Mike L. Feb 01 '12 at 12:58
  • @MikeL.: Daniel is right though. Perhaps you haven't set up apiCall correctly? Is there a stack trace going witht he null reference exception that tells you exactly where the null is in case it is from inside that apiCall object (eg because you passed in a null context or similar)... – Chris Feb 01 '12 at 13:04
  • @MikeL.: Care to show the complete code between `Dim apiCall As GetSellerTransactionsCall = New GetSellerTransactionsCall(apiContext)` and `Transactions = apiCall.GetSellerTransactions("1/1/2012 5:51:09 AM", "1/30/2012 5:51:09 AM")`? – Daniel Hilgarth Feb 01 '12 at 13:05
  • ok theres the code and some notes (i commented the instruction which causes the fault) – Mike L. Feb 01 '12 at 13:15
  • @MikeL.: As Chris noted, maybe the exception happens inside the library because `apiContext` is Nothing. Please check. Furthermore, you might have issues with multiple threads running in parallel... – Daniel Hilgarth Feb 01 '12 at 13:20
  • Actually, i have a function GetApiContext() that is called to give ApiContext a value...i updated the code again – Mike L. Feb 01 '12 at 15:36
  • 1
    WHOA...left out this line `Dim apiContext As ApiContext = GetApiContext()` directly before `Dim apiCall As GetSellerTransactionsCall = New GetSellerTransactionsCall(apiContext)` let me see if this fixes it (it should) Sorry I've been up all night coding – Mike L. Feb 01 '12 at 15:37
  • That fixed it. Thanks for your help, since the comments here solved the problem and offered insight into other problems people may have with the same error, I chose this answer as accepted. – Mike L. Feb 01 '12 at 16:27