- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这应该很简单,但我遇到了问题。使用 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
Name
属性有一个成功存储到其变量中的值,而
Order.ShippingAddress.CompanyName
设置为
Nothing
(此属性确实存在,并且有时可以具有值)。我更新了代码以包含 Anthony Pegram 的回答,但没有帮助。
Order.ShippingAddress.CompanyName
什么都没有,在这些时候是否可以将其作为空字符串处理?我尝试了 ToString() 方法。在其他语言中,我可以
最佳答案
您可以使用 If(a, b)
将空值合并为另一个值。例子:
Dim obj as String = Nothing
Dim foo as String = If(obj, "foo")
Order
或
ShippingAddress
可能为空。访问空引用上的属性或方法是错误的。简单地通过将空值存储到变量来访问它本身并不是错误。
CompanyName = Order.ShippingAddress.CompanyName
State = Order.ShippingAddress.StateOrProvince
There is updated code please note no exception is thrown until:
BillingCompany = If(Order.ShippingAddress.CompanyName.ToString(), "")
is executed.
BillingCompany = If(Order.ShippingAddress.CompanyName, "")
关于.net - VB .Net NullReferenceException 解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9246177/
当我单步执行以下代码时,第二行的 report 为空。但是,第三行生成 NullReferenceException。 member this.setTaggedResearchReportList
我正在尝试按照建议从 URL 获取查询字符串 here ,但我收到 NullReferenceException。我的代码与链接帖子中的代码之间的唯一区别是我的代码是静态的,我看不出这会如何导致错误。
我有以下程序,这是一段示例代码,展示了 C# 反射如何对类进行操作。一切正常,完全没有问题。 public class Program { public static void Main() {
我在以下代码中使用 F# 类型和数据结构(我在 Mac 上使用 Monodevelop,这仅发生在 Interactive 中): type UnbalancedSet = | E |
我有一个 asp.net/C# 类可以调整图像的大小以作为文件缓存在服务器上,但是确定使用哪个编码器的代码部分似乎偶尔会抛出 NullReferenceException。 下面是初始化和传回编码器的
我已经向几位同事展示了这一点,但没有人给出解释。我纯粹偶然遇到了这个问题,因为我以为我在代码中发现了一个错误,但惊讶地发现代码实际上运行了。这是一个简化版本。这已通过 XE-2 完成。 到目前为止,与
这把我难住了。我试图优化 Noda Time 的一些测试,其中我们进行了一些类型初始值设定项检查。我想在将所有内容加载到新的 AppDomain 之前,我想找出类型是否有类型初始值设定项(静态构造函数
当我的页面加载时,我从 SQLite 数据库加载一个列表,有时在加载时我会得到 NullReferenceException ,并显示错误:对象引用未设置为对象的实例。 它在 SQLite 类文件中破
这个问题已经有答案了: What is a NullReferenceException, and how do I fix it? (26 个回答) 已关闭 4 年前。 我收到以下异常: Unhan
当我的页面加载时,我从 SQLite 数据库加载一个列表,有时当它加载时我得到 NullReferenceException 错误说 Object reference not set to an对象的
我有以下代码: try { using (var stream = new MemoryStream()) { var ms = stream; if (con
这个问题已经有答案了: What is a NullReferenceException, and how do I fix it? (26 个回答) 已关闭 4 年前。 我收到以下异常: Unhan
我有以下代码。 XElement opCoOptOff = doc.Descendants(ns + "OpCoOptOff").FirstOrDefault(); String opCo = o
这个问题在这里已经有了答案: What is a NullReferenceException, and how do I fix it? (27 个答案) 关闭 4 年前。 我有队列代码但抛出 N
所以我正在制作一个应用程序,一切都运行良好,直到发生以下情况: 我已经通过调试器进行了检查,但无法找到导致此异常的确切原因。我感觉根本原因在 SQL 方面,因为这是我最近更改的唯一部分,但我需要确切地
class Puzzle { private int PUZZLESIZE = 3; private int col, row; priva
我有一项学校作业,我快完成了,只剩下一件事了。每次启动程序时,我都会收到 NullReferenceException。除了抛出异常的 ListView 外,一切都按预期工作。 这是来自主窗体: pr
我向 Form1 组件添加了一个面板。该面板名为 panel1。 浏览通用列表时,我想动态添加标签。看看我的小代码: if (list.Count > 0) { foreach (TLClas
这是我的场景:我有一个控制台应用程序,我正在尝试使用以下代码处理控制台的“关闭事件”: static void Main(string[] args) { SetConsoleCtrlHand
这可能已经有人问过了。我搜索了 SO 并发现了一些关于 Null VS String.Empty 的问题,但我很好奇为什么以下语句不会抛出 NullReferenceException: String
我是一名优秀的程序员,十分优秀!