title: VB .Net NullReferenceException work-around
body: <p>This should be simple, but I am having problems. Using the eBay .Net library, sometimes certain fields in a response are <code>Nothing</code> and sometimes contain a value. When they are <code>Nothing</code> they could simple be represented as an empty string, but the developers chose to return them as <code>Nothing</code> so when I try to set a String to the value (when nothing is there) I get a NullReferenceException, see below</p>
  
  <pre><code>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 &amp; ":" &amp; 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, " &amp; _
             "BillingCompany, BillingEmailAddress, BillingPhone, BillingAddress1, " &amp; _
             "BillingAddress2, BillingCity, BillingStateProvidence, BillingPostalCode, " &amp; _
             "BillingCountry, ShippingFirstName, ShippingLastName, ShippingCompany, " &amp; _
             "ShippingEmailAddress, ShippingPhone, ShippingAddress1, ShippingAddress2, " &amp; _
             "ShippingCity, ShippingStateProvidence, ShippingPostalCode, ShippingCountry, " &amp; _
             "OrderStatus, BillingStatus, OrderDate, SalesTax, ShippingMethod, OrderTotalCharged, OrderChannel) " &amp; _
             "VALUES('" &amp; OrderID &amp; "', '" &amp; AccountID &amp; "', '" &amp; BillingFirstName &amp; "', '" &amp; _
             BillingLastName &amp; "', '" &amp; BillingCompany &amp; "', '" &amp; BillingEmailAddress &amp; "', '" &amp; _
             BillingPhone &amp; "', '" &amp; BillingAddress1 &amp; "', '" &amp; BillingAddress2 &amp; "', '" &amp; BillingCity &amp; "', '" &amp; _
             BillingStateProvidence &amp; "', '" &amp; BillingPostalCode &amp; "', '" &amp; BillingCountry &amp; "', '" &amp; _
             ShippingFirstName &amp; "', '" &amp; ShippingLastName &amp; "', '" &amp; ShippingCompany &amp; "', '" &amp; _
             ShippingEmailAddress &amp; "', '" &amp; ShippingPhone &amp; "', '" &amp; ShippingAddress1 &amp; "', '" &amp; _
             ShippingAddress2 &amp; "', '" &amp; ShippingCity &amp; "', '" &amp; ShippingStateProvidence &amp; "', '" &amp; _
             ShippingPostalCode &amp; "', '" &amp; ShippingCountry &amp; "', '" &amp; OrderStatus &amp; "', '" &amp; _
             BillingStatus &amp; "', '" &amp; OrderDate &amp; "', '" &amp; SalesTax &amp; "', '" &amp; ShippingMethod &amp; "', '" &amp; _
             OrderTotalCharged &amp; "', '" &amp; OrderChannel &amp; "')"
      ' Dim orderResult As Integer = comm.ExecuteNonQuery()
      sysLog.WriteEntry(comm.CommandText)
  End Sub
  End Class
  </code></pre>
  
  <p>There is updated code please note no exception is thrown until:
      BillingCompany = If(Order.ShippingAddress.CompanyName.ToString(), "")
  is executed. The <code>Name</code> property has a value which successfully stored into its variable, while <code>Order.ShippingAddress.CompanyName</code> is set to <code>Nothing</code> (this property does exist, and can sometimes have a value). I updated the code to include Anthony Pegram's answer which did not help.</p>
  
  <p>Everything is declared properly, to shorten the code I just showed a relevant example. Consider this inside of a loop for each order returned from a GetOrders() call, sometimes the <code>Order.ShippingAddress.CompanyName</code> will be nothing, at these times is it possible to handle it as an empty string? I tried the ToString() method. In other languages I could 
      $CompanyName = this || that;</p>
  
  <p>Anything similar in VB .Net?</p>