I am implementing a procedure for creating an XML file for the transmission of some data, using VS 2019 vb.net. To do this, I have built a series of classes as suggested by Microsoft. The procedure compiles correctly but at run time generates an error like 'Object reference not set on an object instance.' to the line
FileAbaco.utenti(0).utente(0).strutture(0).struttura.Add(stra)
I can't understand where I'm wrong. Thank you in advance for any help you will be able to provide.
Dim FileAbaco As New dichiarazioni
FileAbaco.comune = "I608"
Dim uti As New utenti
FileAbaco.utenti.Add(uti)
Dim ute As New utente
FileAbaco.utenti(0).utente.Add(ute)
Dim stre As New strutture
FileAbaco.utenti(0).utente(0).strutture.Add(stre)
Dim stra As New struttura
FileAbaco.utenti(0).utente(0).strutture(0).struttura.Add(stra)
Dim dr As DataRow
dr = DTTrimestrale.Rows(0)
With FileAbaco.utenti(0).utente(0)
.auth = dr.Item("auth".ToString)
.gest_codice_fiscale = dr.Item("gest_codice_fiscale")
.codice_utente = dr.Item("codice_utente")
.gest_cognome = dr.Item("gest_cognome")
.gest_nome = ""
End With
These are the classes:
Imports System.Xml.Serialization
Public Class dichiarazioni
Public comune As String
<XmlElement()>
Public Property utenti As New List(Of utenti)
End Class
Public Class utenti
<XmlElement()>
Public Property utente As New List(Of utente)
End Class
Public Class utente
Public Property auth As String
Public Property gest_codice_fiscale As String
Public Property codice_utente As String
Public Property gest_cognome As String
Public Property gest_nome As String
Public Property strutture As New List(Of strutture)
Public Sub New()
End Sub
Public Sub New(auth As String, gest_codice_fiscale As String, codice_utente As String, gest_cognome As String, gest_nome As String, strutture As strutture)
Me.auth = auth
Me.gest_codice_fiscale = gest_codice_fiscale
Me.codice_utente = codice_utente
Me.gest_cognome = gest_cognome
Me.gest_nome = gest_nome
Me.strutture.Add(strutture)
End Sub
End Class
Public Class strutture
<XmlElement()>
Public Property struttura As List(Of struttura)
Public Sub New()
End Sub
Public Sub New(struttura As struttura)
Me.struttura.Add(struttura)
End Sub
End Class
Public Class struttura
<XmlElement()>
Public Property ID_struttura As String
Public Property insegna As String
Public Sub New()
End Sub
Public Sub New(ID_struttura As String, insegna As String)
Me.ID_struttura = ID_struttura
Me.insegna = insegna
End Sub
End Class
CodePudding user response:
Obviously it’s right because you have to initialize struttura
as it is always Nothing
.
Cambia questa classe come segue:
Public Class strutture
<XmlElement()>
Public Property struttura As List(Of struttura)
Public Sub New()
If Me.struttura Is Nothing Then Me.struttura = New List(Of struttura)
End Sub
Public Sub New(struttura As struttura)
If Me.struttura Is Nothing Then Me.struttura = New List(Of struttura)
Me.struttura.Add(struttura)
End Sub
End Class
Otherwise: Altrimenti fai come sopra (nella classe che precede)
Public Property whatEver As New List(Of Whatever)