gpt4 book ai didi

.net - VB .Net NullReferenceException 解决方法

转载 作者:行者123 更新时间:2023-12-04 13:34:05 26 4
gpt4 key购买 nike

这应该很简单,但我遇到了问题。使用 eBay .Net 库,有时响应中的某些字段是 Nothing有时包含一个值。当他们Nothing它们可以简单地表示为一个空字符串,但开发人员选择将它们返回为 Nothing因此,当我尝试将 String 设置为该值时(当没有任何内容时)我得到一个 NullReferenceException,见下文

Imports System
Imports System.Configuration.ConfigurationManager
Imports System.Globalization
Imports System.Threading
Imports System.Xml

Imports eBay.Service.Core.Soap
Imports eBay.Service.Core.Sdk
Imports eBay.Service.Call
Imports eBay.Service.Util

Public Class eBayOrderImportService
Private Sub ServiceWorkerThread(ByVal state As Object)
' Periodically check if the service is stopping.
Do While Not Me.stopping
' Perform main service function here...
GetLastTime()
Dim apiContext As ApiContext = GetApiContext()

Dim apiCall As GetOrdersCall = New GetOrdersCall(apiContext)
Dim orders As New OrderTypeCollection

Dim timeFilter As New TimeFilter
timeFilter.TimeFrom = lastUpdate
timeFilter.TimeTo = Date.Now


Dim lastTime As Boolean = SetLastTime()
apiCall.IncludeFinalValueFee = True
orders = apiCall.GetOrders(timeFilter, TradingRoleCodeType.Seller, OrderStatusCodeType.Completed)

Dim order As OrderType

For Each order In orders
'do order-wide stuff here
LogOrder(order)

Next
Thread.Sleep(30 * 1000) ' Simulate some lengthy operations.

Loop

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

Private Sub LogOrder(ByVal Order As OrderType)

Dim OrderID, AccountID, BillingFirstName, BillingLastName, _
BillingCompany, BillingEmailAddress, BillingPhone, _
BillingAddress1, BillingAddress2, BillingCity, _
BillingStateProvidence, BillingPostalCode, _
BillingCountry, ShippingFirstName, ShippingLastName, _
ShippingCompany, ShippingEmailAddress, ShippingPhone, _
ShippingAddress1, ShippingAddress2, ShippingCity, _
ShippingStateProvidence, ShippingPostalCode, _
ShippingCountry, OrderStatus, BillingStatus, _
OrderDate, ShippingMethod, SalesTax, _
PreShippingCharge, OrderDiscount, OrderTotalCharged, _
PaymentMethod, RepeatOrder, GiftCode, CouponCode, RID, _
OrderNotes, OrderChannel, IsPrinted, IsShipped, PrintDate, _
ShipDate, ActualShipCharge, DaysInTransit, DeliveryDate, _
TrackingNumber, ShippedMethod As String


OrderID = Order.OrderID

AccountID = ""

Dim name As String = If(Order.ShippingAddress.Name.ToString(), "None Given")

BillingFirstName = name.Substring(0, name.IndexOf(" "))
BillingLastName = name.Substring(name.IndexOf(" ") + 1)
BillingCompany = If(Order.ShippingAddress.CompanyName.ToString(), "")
BillingEmailAddress = If(Order.TransactionArray(0).Buyer.Email.ToString(), "")
BillingPhone = If(Order.ShippingAddress.Phone.ToString(), "")
BillingAddress1 = If(Order.ShippingAddress.Street1.ToString(), "")
BillingAddress2 = If(Order.ShippingAddress.Street2.ToString(), "")
BillingCity = If(Order.ShippingAddress.CityName.ToString(), "")
BillingStateProvidence = If(Order.ShippingAddress.StateOrProvince.ToString(), "")
BillingPostalCode = If(Order.ShippingAddress.PostalCode.ToString(), "")
BillingCountry = If(Order.ShippingAddress.CountryName.ToString(), "")
ShippingFirstName = If(BillingFirstName, "")
ShippingLastName = If(BillingLastName, "")
ShippingCompany = If(Order.ShippingAddress.CompanyName.ToString(), "")
ShippingEmailAddress = If(Order.TransactionArray(0).Buyer.Email.ToString(), "")
ShippingPhone = If(Order.ShippingAddress.Phone.ToString(), "")
ShippingAddress1 = If(Order.ShippingAddress.Street1.ToString(), "")
ShippingAddress2 = If(Order.ShippingAddress.Street2.ToString(), "")
ShippingCity = If(Order.ShippingAddress.CityName.ToString(), "")
ShippingStateProvidence = If(Order.ShippingAddress.StateOrProvince.ToString(), "")
ShippingPostalCode = If(Order.ShippingAddress.PostalCode.ToString(), "")
ShippingCountry = If(Order.ShippingAddress.CountryName.ToString(), "")
OrderStatus = If(Order.OrderStatus.ToString(), "")
BillingStatus = If(Order.OrderStatus.ToString(), "")
OrderDate = If(Order.CreatedTime.ToString("MM/DD/yyyy"), "")
If Order.TransactionArray(0).Taxes IsNot Nothing Then
Dim tmpTax As Double = 0.0
Dim tmpTrans As TransactionType
For Each tmpTrans In Order.TransactionArray
tmpTax = tmpTax + tmpTrans.Taxes.TotalTaxAmount.Value
Next
SalesTax = tmpTax.ToString()

Else
SalesTax = "0.0"
End If


ShippingMethod = If(Order.ShippingServiceSelected.ShippingService.ToString(), "")
ShippingMethod = ShippingMethod & ":" & If(Order.ShippingServiceSelected.ShippingServicePriority.ToString(), "")

OrderTotalCharged = If(Order.Total.Value.ToString(), "")
OrderChannel = "eBay"
comm = New OdbcCommand
comm.CommandText = _
"INSERT INTO Orders (OrderID, AccountID, BillingFirstName, BillingLastName, " & _
"BillingCompany, BillingEmailAddress, BillingPhone, BillingAddress1, " & _
"BillingAddress2, BillingCity, BillingStateProvidence, BillingPostalCode, " & _
"BillingCountry, ShippingFirstName, ShippingLastName, ShippingCompany, " & _
"ShippingEmailAddress, ShippingPhone, ShippingAddress1, ShippingAddress2, " & _
"ShippingCity, ShippingStateProvidence, ShippingPostalCode, ShippingCountry, " & _
"OrderStatus, BillingStatus, OrderDate, SalesTax, ShippingMethod, OrderTotalCharged, OrderChannel) " & _
"VALUES('" & OrderID & "', '" & AccountID & "', '" & BillingFirstName & "', '" & _
BillingLastName & "', '" & BillingCompany & "', '" & BillingEmailAddress & "', '" & _
BillingPhone & "', '" & BillingAddress1 & "', '" & BillingAddress2 & "', '" & BillingCity & "', '" & _
BillingStateProvidence & "', '" & BillingPostalCode & "', '" & BillingCountry & "', '" & _
ShippingFirstName & "', '" & ShippingLastName & "', '" & ShippingCompany & "', '" & _
ShippingEmailAddress & "', '" & ShippingPhone & "', '" & ShippingAddress1 & "', '" & _
ShippingAddress2 & "', '" & ShippingCity & "', '" & ShippingStateProvidence & "', '" & _
ShippingPostalCode & "', '" & ShippingCountry & "', '" & OrderStatus & "', '" & _
BillingStatus & "', '" & OrderDate & "', '" & SalesTax & "', '" & ShippingMethod & "', '" & _
OrderTotalCharged & "', '" & OrderChannel & "')"
' Dim orderResult As Integer = comm.ExecuteNonQuery()
sysLog.WriteEntry(comm.CommandText)
End Sub
End Class

有更新的代码请注意,直到:
BillingCompany = If(Order.ShippingAddress.CompanyName.ToString(), "")
被执行。 Name属性有一个成功存储到其变量中的值,而 Order.ShippingAddress.CompanyName设置为 Nothing (此属性确实存在,并且有时可以具有值)。我更新了代码以包含 Anthony Pegram 的回答,但没有帮助。

一切都被正确声明,为了缩短代码,我刚刚展示了一个相关的例子。在 GetOrders() 调用返回的每个订单的循环内考虑这一点,有时是 Order.ShippingAddress.CompanyName什么都没有,在这些时候是否可以将其作为空字符串处理?我尝试了 ToString() 方法。在其他语言中,我可以
$CompanyName = this ||那;

VB .Net 中有类似的东西吗?

最佳答案

您可以使用 If(a, b)将空值合并为另一个值。例子:

Dim obj as String = Nothing
Dim foo as String = If(obj, "foo")

本例中 foo 的输出将是字符串“foo”。如果分配给 obj 的非空字符串,则 foo 也会引用该字符串。

但是,我有一种强烈的感觉,您的空引用异常可能不是发生在属性上,而是发生在对象上。 OrderShippingAddress可能为空。访问空引用上的属性或方法是错误的。简单地通过将空值存储到变量来访问它本身并不是错误。

如果您在其中一条线路上遇到异常
CompanyName = Order.ShippingAddress.CompanyName 
State = Order.ShippingAddress.StateOrProvince

这是因为 Order 或 ShippingAddress 为空。

检查并查看这些对象是否实际上可以作为空返回。如果是这样,您将需要在访问它们的属性之前对它们应用空检查。

您的更新:

There is updated code please note no exception is thrown until:

BillingCompany = If(Order.ShippingAddress.CompanyName.ToString(), "")

is executed.



这可能会抛出,因为 Order 可以为 null、ShippingAddress 可以为 null 或 CompanyName 可以为 null。 访问空引用上的属性或方法是一个异常(exception)。 如果 Order 为空,您将在访问 ShippingAddress 时遇到错误。同样,如果 ShippingAddress 为 null,则无法访问 CompanyName。如果 CompanyName 为空,则不能调用 ToString()。

您必须在某个时候验证哪个对象为空。我不相信它们中的任何一个都不会为空。你?查看。逐步执行您的程序并观察对象状态。

如果确实归结为 CompanyName 之类的属性为 null,并且 CompanyName 是字符串,则省略 ToString() 调用。
BillingCompany = If(Order.ShippingAddress.CompanyName, "")

如果 CompanyName 不是字符串,则只需在调用 ToString() 或访问属性之前将其与 Nothing 进行比较。

关于.net - VB .Net NullReferenceException 解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9246177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com