Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have write my code in vb.net. I have class shipping_address.
In the loop statement, the first data there is no any address. How to check nothing then assign the value for address1 ? Whenever i run i am getting null reference exception.

For Each obj In st.orders
            Dim RefNo As Long = obj.id
            Dim OrderDate As String = obj.created_at
            Dim TotalAmt As Double = obj.total_price
            Dim Email As String = obj.email
            Dim CustName As String = obj.name

            Dim TotalTax As Double = obj.total_tax
            Dim TotalWeight As Long = obj.total_weight
            Dim Remarks As String = obj.note
            Dim FirstName As String = obj.customer.first_name
            Dim LastName As String = obj.customer.last_name
            Dim ShipCode As String = obj.shipping_lines(0).code
            Dim ShipPrice As Double = obj.shipping_lines(0).price
            Dim SAddress As String= obj.shipping_address.address1


Next


What I have tried:

Dim SAddress As String
            If obj.shipping_address IsNot Nothing AndAlso _
            obj.shipping_address.address1 = String.Empty Then
                obj.shipping_address.address1 = SAddress
            End If
Posted
Updated 28-Sep-20 18:44pm

1 solution

Quote:
How to check nothing then assign the value for address1 ?

Try with nullable operators:
VB
' Nothing if customers, the first customer, or Orders is Nothing
Dim count As Integer? = customers?(0)?.Orders?.Count()

is same as:
VB
Dim length As Integer
If customers IsNot Nothing Then
   length = customers.Length
End If


Refer: Null-conditional Operators - Visual Basic | Microsoft Docs[^]

Your case, try something like:
VB
Dim SAddress As String? = obj?.shipping_address.address1
 
Share this answer
 
Comments
Member 12611727 29-Sep-20 1:10am    
thank you it worked :)
Sandeep Mewara 29-Sep-20 10:47am    
Good to know. :thumbsup:
CPallini 29-Sep-20 2:31am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900