嗯,eBay SDK有很多很好的例子,但是它们已经过时了,大多数都行不通,你就得到了NullReferenceException
。我是Windows程序员大约5年(5年前就已经知道了。只有足够的.Net才能让我过去。我开发了大多数基于Web的大型应用程序)
此特定应用程序以给定的时间间隔通过Windows服务轮询eBay API,并使用待发送的待处理订单更新SQL数据库。这不是必需的,因为这段代码是直截了当而不是问题。
以下是有问题的旧VB .Net代码行,请记住,intelliSense会在代码视图中将代码显示为有效。
Dim Transactions As TransactionTypeCollection
Transactions = apiCall.GetSellerTransactions("1/1/2012 5:51:09 AM", "1/30/2012 5:51:09 AM")
当第二行代码运行时,我收到此错误:
NullReferenceException was unhandled
Object reference not set to an instance of an object.
Visual Studio提供了一些故障排除技巧,例如确保在调用之前设置的对象不是NULL(Nothing),并在调用方法之前使用New关键字创建对象的新实例。我尝试了这些方法的所有组合:
Dim Transactions As New Transaction TypeCollection
或在定义交易后,
Transactions = New apicall.getSellerTransaction()
'didnt think this would work but I've tried everything
这些没有帮助,第一个也没有产生任何额外的错误(第二个,因为我假设让你知道getSellerTransaction()不是构造函数)。
有什么建议吗?
感谢阅读长篇文章,只是想尽可能彻底。 BTW我正在使用developer.ebay.com的最新eBay .NET SDK尝试执行getSellerTransaction。生成令牌时我遇到了类似问题,但修复方法不同。我认为这是一个语法错误。谢谢你的帮助。如果您需要更多细节,我会在这里回答任何问题。
-Mike
附加代码
我正在使用一个简单的编写器从事务中捕获足够的数据,以便我知道它们有效(当我遇到这个错误时,挂起的订单将被填充到一个sql数据中)。这也是一个Windows服务(因此theServiceWorkerThread
)此外,eBay SDK中提供的.Net演示应用程序(至少GetSellerTransactions失败,错误代码相同,位置相同)
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
''” '''使用应用程序配置文件中的数据填充eBay SDK ApiContext实例 “”” '''ApiContext实例 '''
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
答案 0 :(得分:3)
问题不是Transactions
Nothing
而apiCall
是Nothing
。
确保将apiCall
初始化为正确的值。