Problème ajout objet dans fichier xml.
Bonjour à tous.
Je vais aller droit au but, j'ai ce code :
Code:
1 2 3 4 5 6
| Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Nom_Client.Feuille.Add(Feuilles)
Enregistre_Fiche_Client(TB1.Text & ".xml", Nom_Client)
End Sub |
dans ma form et ce code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| Public Erreur As Boolean = False 'Créé un booléen "Erreur" de valeur 0 (Booléen = 0 ou 1)'
Public Nom_Client As New Client
Public Adresse_Mail_Client As New Adresse_Mail
Public Téléphone_Client As New Téléphone
Public Interventions_Client As New Interventions
Public Machines_Client As New Machine
Public Matériel_Client As New Matériel
Public Feuilles As New Feuille
Function Ouverture_Fiche_Client(ByVal filename As String) As Client 'Créé la fonction lecture du fichier xml'
Try 'Essaye le code suivant :'
Using objStreamReader As New StreamReader(filename) 'Utilise le flux de données du fichier "filename"'
Try 'Essaye le code suivant : '
Return DirectCast((New XmlSerializer(GetType(Client))).Deserialize(objStreamReader), Client) 'Sérialise le fichier xml'
Catch ex As Exception 'Si l'essais échoue'
Erreur = True 'Met le booléen en valeur "vrai"'
MessageBox.Show("Echec de la lecture." & ex.Message) 'Affiche un message d'erreur'
End Try 'Fin de l'essais du code'
End Using 'Coupe toutes ressources utilisées pour créer le flux.'
Catch ex As Exception 'Si le fichier n'existe pas'
Erreur = True 'Met le booléen en valeur "vrai"'
MessageBox.Show("Fichier inaccessible." & ex.Message) 'Affiche un message contenant l'erreur.'
End Try 'Fin de l'essais du code'
Return Nothing
End Function
Sub Enregistre_Fiche_Client(ByVal filename As String, Nom_Client As Client)
Try
Using objStreamWriter As StreamWriter = New StreamWriter(filename)
Try
Dim xsPropriety As New XmlSerializer(GetType(Client), New XmlRootAttribute("Client"))
xsPropriety.Serialize(objStreamWriter, Nom_Client)
Catch ex As Exception
Erreur = True
MessageBox.Show("Echec de l'écriture." & ex.Message)
End Try
End Using
Catch ex As Exception
Erreur = True
MessageBox.Show("Fichier inaccessible." & ex.Message)
End Try
End Sub
End Module
<XmlRoot("Client")>
Public Class Client
<XmlElement("Société")>
Public Property Société As New Société
<XmlElement("Interventions")>
Public Property Interventions As List(Of Interventions)
<XmlElement("Feuille")>
Public Property Feuille As List(Of Feuille)
End Class
Public Class Société
<XmlAttribute("Nom")>
Public Property Nom As String = ""
<XmlElement("Adresse")>
Public Property Adresse As New Adresse
End Class
Public Class Adresse
<XmlAttribute("Ville")>
Public Property Ville As String = ""
<XmlAttribute("C_Postal")>
Public Property Code_Postal As String = ""
<XmlAttribute("Rue")>
Public Property Rue As String = ""
<XmlElement("Téléphone")>
Public Property Téléphone As List(Of Téléphone)
<XmlElement("Adresse_Mail")>
Public Property Adresse_Mail As List(Of Adresse_Mail)
End Class
Public Class Interventions
<XmlAttribute("Num_Facture")>
Public Property Num_Facture As String = ""
<XmlAttribute("Date")>
Public Property Date1 As String = ""
<XmlElement("Machine")>
Public Property Machine As List(Of Machine)
End Class
Public Class Machine
<XmlElement("Matériel")>
Public Property Matériel As List(Of Matériel)
End Class
Public Class Matériel
<XmlAttribute("Type_Produit")>
Public Property Type_Produit As String = ""
<XmlElement("Référence")>
Public Property Référence As New Référence
End Class
Public Class Référence
<XmlAttribute("Num_Série")>
Public Property Num_Série As String = ""
<XmlAttribute("V_Input")>
Public Property Tension_Entrée As String = ""
<XmlAttribute("V_Output")>
Public Property Tension_Sortie As String = ""
End Class
Public Class Feuille
<XmlAttribute("Nom")>
Public Property Nom As String = "Divers"
End Class
Public Class Téléphone
<XmlAttribute("Numéro")>
Public Property Numéro As String = ""
<XmlAttribute("Nom")>
Public Property Nom As String = ""
End Class
Public Class Adresse_Mail
<XmlAttribute("Mail")>
Public Property Mail As String = ""
<XmlAttribute("Nom")>
Public Property Nom As String = ""
End Class |
Dans un module.
Mon problème est que je ne peux pas ajouter quoi que ce soit (en l’occurrence des feuilles) :
lorsque l’exécution en arrive à cette ligne :
Code:
Nom_Client.Feuille.Add(Feuilles)
Il me met ce message d'erreur :
Une exception non gérée du type 'System.NullReferenceException' s'est produite dans WindowsApplication1.exe
Informations supplémentaires : La référence d'objet n'est pas définie à une instance d'un objet.
Je ne comprends pas d'ou vient cette erreur. Merci de votre aide