|
-
Aug 27th, 2023, 04:43 PM
#1
Thread Starter
Member
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
-
Aug 28th, 2023, 08:51 AM
#2
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
-
Aug 29th, 2023, 09:50 AM
#3
Thread Starter
Member
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
-
Aug 29th, 2023, 10:25 AM
#4
Re: VB.Net Object creation do JSON API submission
 Originally Posted by dashley
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)
-
Aug 29th, 2023, 11:09 AM
#5
Thread Starter
Member
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)()
-
Aug 29th, 2023, 11:14 AM
#6
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.
-
Aug 29th, 2023, 12:57 PM
#7
Thread Starter
Member
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
-
Aug 29th, 2023, 02:20 PM
#8
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.
-
Aug 29th, 2023, 06:33 PM
#9
Thread Starter
Member
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
-
Aug 30th, 2023, 03:04 AM
#10
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.
-
Aug 30th, 2023, 11:21 AM
#11
Thread Starter
Member
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")
-
Aug 30th, 2023, 11:36 AM
#12
Lively Member
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")
-
Aug 30th, 2023, 12:32 PM
#13
Thread Starter
Member
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.'
-
Aug 30th, 2023, 01:00 PM
#14
Lively Member
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
-
Aug 30th, 2023, 01:58 PM
#15
Re: VB.Net Object creation do JSON API submission
 Originally Posted by dashley
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.
-
Aug 31st, 2023, 12:49 PM
#16
Thread Starter
Member
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.
-
Sep 2nd, 2023, 10:12 AM
#17
Thread Starter
Member
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.
-
Sep 2nd, 2023, 10:00 PM
#18
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|