【问题标题】:Need help converting this one line of code from vb .net 2005~ to vb .net 2010 (eBay .Net SDK)需要帮助将这一行代码从 vb .net 2005~ 转换为 vb .net 2010 (eBay .Net SDK)
【发布时间】:2012-02-24 03:04:57
【问题描述】:

嗯,eBay SDK 有很多不错的示例可供选择,但它们已经过时,大多数都无法正常工作,您可以在线获得NullReferenceException。我做了大约 5 年的 Windows 编码员(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() 不是构造函数)。

有什么建议吗?

感谢您阅读这篇长文,只是想尽可能详尽。顺便说一句,我正在使用来自 developer.ebay.com 的最新 eBay .NET SDK 尝试执行 getSellerTransaction。我在生成令牌时遇到了类似的问题,但修复方法不同。我认为这是一个语法错误。谢谢你的帮助。如果您需要更多详细信息,我会在这里回答任何问题。

-迈克

附加代码

我正在使用一个简单的流编写器从事务中捕获足够的数据,以便我知道它们可以工作(当我克服这个错误时,待处理的订单将被填充到一个 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

<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

【问题讨论】:

    标签: .net vb.net nullreferenceexception ebay-api ebay-net-sdk


    【解决方案1】:

    问题不是TransactionsNothing 而是apiCallNothing

    确保apiCall 已初始化为正确的值。

    【讨论】:

    • Dim apiCall As GetSellerTransactionsCall = New GetSellerTransactionsCall(apiContext)
    • 这有一个值,而且是正确的。相同的代码在其他可以正常工作的地方使用。
    • @MikeL.:丹尼尔是对的。也许您没有正确设置 apiCall?是否有一个堆栈跟踪与空引用异常一起告诉您空值的确切位置,以防它来自该 apiCall 对象内部(例如,因为您传入了空上下文或类似内容)...
    • @MikeL.:在Dim apiCall As GetSellerTransactionsCall = New GetSellerTransactionsCall(apiContext)Transactions = apiCall.GetSellerTransactions("1/1/2012 5:51:09 AM", "1/30/2012 5:51:09 AM") 之间显示完整的代码?
    • 哇...在Dim apiCall As GetSellerTransactionsCall = New GetSellerTransactionsCall(apiContext) 之前直接省略了这一行Dim apiContext As ApiContext = GetApiContext() 让我看看这是否解决了它(它应该)对不起,我已经通宵编码了
    猜你喜欢
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    相关资源
    最近更新 更多