Results 1 to 18 of 18

Thread: VB.Net Object creation do JSON API submission

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    VB.Net Object creation do JSON API submission

    Hi I'm trying to create an object/array to add a "code" "HAZ" to the Accessorial class shown below.
    I don't know how to create it to add the code. I've added the accessorial class to the primary wrapper but am uncertain if if should be added as "Public accessorials As New Accessorials"
    or Public accessorials As New list(Of Accessorials).

    In the Accesorial class the code is shown as " Public codes As String()" which I'm assuming it can be a string array because there are time when more than one code can be submittied.

    the JSON outcome (patrial) I'm looking for is according to the API guide is:

    Code:
    "accessorials" : {
    "codes" : [ "HAZ" ],
    "hazardousContact" : {
    "name" : "John Snow",
    "phone" : "7704865900"
    }


    Code:
       Public Class fgtload 'Primary wrapper
            Public service As New fgtservice
            Public payment As New fgtpayment
            Public transit As New fgttransit
            Public commodities As New List(Of Items)
            Public origin As New fgtorigin
            Public destination As New fgtdestination
            Public billTo As New fgtbillto
            Public accessorials As New Accessorials
        End Class
    
        Public Class Accessorials
            Public codes As String()
            Public hazardousContact As HazardousContact
            Public cod As Object
            Public insuranceDetails As Object
            Public sortAndSegregateDetails As Object
            Public markDetails As Object
        End Class

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: VB.Net Object creation do JSON API submission

    I don't know if I agree with initializing the properties like that. Personally, I'd define them and then initialize them in the class' constructor.

    Regarding the definition of codes, it depends. If you're manipulating the data a lot from VB.NET then I'd probably define it as a list.

    Take a look at this example:
    Code:
    Public Class fgtload
        Public service As fgtservice
        Public payment As fgtpayment
        Public transit As fgttransit
        Public commodities As List(Of Items)
        Public origin As fgtorigin
        Public destination As fgtdestination
        Public billTo As fgtbillto
        Public accessorials As Accessorials
    
        Sub New()
            service = New fgtservice()
            payment = New fgtpayment()
            transit = New fgttransit()
            commodities = New List(of Items)()
            origin = New fgtorigin()
            destination = New fgtdestination()
            billTo = New billTo()
            accessorials = New Accessorials()
        End Sub
    End Class
    
    Public Class Accessorials
        Public codes As List(Of String)
        Public hazardousContact As HazardousContact
        Public cod As Object 
        Public insuranceDetails As Object
        Public sortAndSegregateDetails As Object
        Public markDetails As Object
    
        Sub New()
            codes = New List(Of String)()
        End Sub
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: VB.Net Object creation do JSON API submission

    The properties and classes were created with a JSON class generator. I'm trying to learn more about JSON. I think I have discovered my weakness. I'm no good with arrays sigh... Yes I'll be manipulating data from vb.net via MSSQL and datasets. I can't seem to grasp how to add the codes into the array and wrap them in the accessorials. I've tried ..

    Code:
    Dim accessorials = New Accessorials()
    
            Dim codes = New List(Of String)()
            Dim mycode As String = ("HAZ")
    
            accessorials.codes(1).Add(mycode)

    WHen I run My request it fails at with an error of ": 'Object reference not set to an instance of an object.'" at
    accessorials.codes(1).Add(mycode).

    Can you provide and example of how to set the "HAZ" code into the array. I'm getting my butt kicked here.

    Thanks

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: VB.Net Object creation do JSON API submission

    Quote Originally Posted by dashley View Post
    The properties and classes were created with a JSON class generator. I'm trying to learn more about JSON. I think I have discovered my weakness. I'm no good with arrays sigh... Yes I'll be manipulating data from vb.net via MSSQL and datasets. I can't seem to grasp how to add the codes into the array and wrap them in the accessorials. I've tried ..

    Code:
    Dim accessorials = New Accessorials()
    
            Dim codes = New List(Of String)()
            Dim mycode As String = ("HAZ")
    
            accessorials.codes(1).Add(mycode)

    WHen I run My request it fails at with an error of ": 'Object reference not set to an instance of an object.'" at
    accessorials.codes(1).Add(mycode).

    Can you provide and example of how to set the "HAZ" code into the array. I'm getting my butt kicked here.

    Thanks
    Code:
       accessorials.codes(1).Add(mycode)
    Is trying to access the first item in the List(Of String) and then add to that - you probably just want to do
    Code:
       accessorials.codes.Add(mycode)

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: VB.Net Object creation do JSON API submission

    When I tired "accessorials.codes.Add(mycode)" It underlines in red (VS 2022) stating 'Add' is not a member of List(Of String)()

  6. #6
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: VB.Net Object creation do JSON API submission

    Could you post the definition of Accessorials that you are currently using?

    If in the snippet
    Code:
       accessorials.codes(1).
    accessorials.codes is an array then you would need to assign a value to accessorials.codes(1) before you can use it. I would use a List(of String) rather than an array if possible.
    Last edited by PlausiblyDamp; Aug 29th, 2023 at 11:27 AM.

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: VB.Net Object creation do JSON API submission

    Code:
     Dim accessorials = New Accessorials()
    
            Dim codes = New List(Of String)()
    
            Dim mycode As String = ("HZM")
    
            accessorials.codes(1).Add(mycode)

    with the following classes for the accessorials code

    Code:
        Public Class fgtload 'Primary wrapper
            Public service As New fgtservice
            Public payment As New fgtpayment
            Public transit As New fgttransit
            Public commodities As New List(Of FgtCommodity)
            Public origin As New fgtorigin
            Public destination As New fgtdestination
            Public billTo As New fgtbillto
            Public accessorials As List(Of Accessorials)
    
    
        End Class
    
        Public Class Accessorials
            Public codes As List(Of String)()
            Public hazardousContact As HazardousContact
            Public cod As Object
            Public insuranceDetails As Object
            Public sortAndSegregateDetails As Object
            Public markDetails As Object
        End Class

  8. #8
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: VB.Net Object creation do JSON API submission

    Code:
       Public codes As List(Of String)()
    in the Accessorials class is declaring the property code as an Array of type List(Of String)

    Try just using
    Code:
       Public codes As List(Of String)
    instead.

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: VB.Net Object creation do JSON API submission

    Dim accessorials As New Accessorials

    Load.accessorials.codes(0) = ("EDP")

    'Object reference not set to an instance of an object.'

    Code:
       Public Class fgtload 'Primary wrapper
            Public service As New fgtservice
            Public payment As New fgtpayment
            Public transit As New fgttransit
            Public commodities As New List(Of FgtCommodity)
            Public origin As New fgtorigin
            Public destination As New fgtdestination
            Public billTo As New fgtbillto
            Public accessorials As New Accessorials
            'Public accessorials As New List(Of Accessorials)
        End Class
    
        Public Class Accessorials
            Public codes As List(Of String)
    
    
            'Public hazardousContact As HazardousContact
            'Public cod As Object
            'Public insuranceDetails As Object
            'Public sortAndSegregateDetails As Object
            'Public markDetails As Object
        End Class

  10. #10
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: VB.Net Object creation do JSON API submission

    Try
    Code:
    Load.accessorials.codes.Add("EDP")
    One of the reasons for using a List(Of ...) is that you can just add items to it without worrying about sizing etc.

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: VB.Net Object creation do JSON API submission

    Still getting System.NullReferenceException: 'Object reference not set to an instance of an object.'

    Dim accessorials = New Accessorials and Load.accessorials = New Accessorials

    **Also tried Both
    With accessorials
    .codes(0) = ("EDP")
    End With
    ** and

    Load.accessorials.codes(0) = ("EDP")

  12. #12
    Lively Member
    Join Date
    Jun 2017
    Posts
    93

    Re: VB.Net Object creation do JSON API submission

    Your Accessorials is an object that has a list of string called Codes.
    You should be able to do something like:

    Load.accessorials.codes = new List(of String)

    Then you can do Load.accessorials.codes.add("something")

  13. #13

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: VB.Net Object creation do JSON API submission

    Tried it same result . Crazy huh

    Load.accessorials.codes = New List(Of String)

    Load.accessorials.codes.Add("EDP")


    System.NullReferenceException: 'Object reference not set to an instance of an object.'

  14. #14
    Lively Member
    Join Date
    Jun 2017
    Posts
    93

    Re: VB.Net Object creation do JSON API submission

    When you do this, you need to make sure your accessorials object actually exists. So before you instantiate codes list, you need to do the same with accessorials object

  15. #15
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: VB.Net Object creation do JSON API submission

    Quote Originally Posted by dashley View Post
    Tried it same result . Crazy huh

    Load.accessorials.codes = New List(Of String)

    Load.accessorials.codes.Add("EDP")


    System.NullReferenceException: 'Object reference not set to an instance of an object.'
    This is when you need to try debugging your code. Put a breakpoint on the line that is crashing, when the breakpoint is hit you can inspect the variables and see which is Nothing, then you can figure out where you should be instantiating it.

  16. #16

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: VB.Net Object creation do JSON API submission

    That cleared the 'Object reference not set to an instance of an object.' error ' Thanks I'm still having issues with the accessorals. GOnna have to put it on hold for a day or so, I shall return :-/
    Last edited by dashley; Aug 31st, 2023 at 06:14 PM.

  17. #17

    Thread Starter
    Member
    Join Date
    Aug 2011
    Posts
    42

    Re: VB.Net Object creation do JSON API submission

    Nothing we tried would have worked anyway, The Code "EDP" which I got from the API User Guide come to find out was an invalid code. I've reported the typo to the web support folks. Never would have thought of that.

    This(with a good code) finally worked.
    Load.accessorials.codes = New List(Of String)

    Load.accessorials.codes.Add("LFTP")

    or this way

    Load.accessorials.codes = New List(Of String)({"IDL"})

    Sure was a marathone getting this resolved though. Learned a lot. Not to trust user guides in part.

  18. #18
    Addicted Member
    Join Date
    Feb 2022
    Posts
    217

    Re: VB.Net Object creation do JSON API submission

    Have you successfully transmitted your JSON request with Curl in Powershell for testing?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width