Search code examples
.netvb.netnullreferenceexceptionebay-apiebay-net-sdk

Need help converting this one line of code from vb .net 2005~ to vb .net 2010 (eBay .Net SDK)


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

Solution

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

    Make sure that apiCall is initialized to a proper value.