Problème sérialization XML d'un ArrayList
Bonjour, je me résigne à poster car je me trouve dans une situation délicate.
Je débute en JAVA et je voudrai réaliser pour commencer une page de connexion qui gèer seulement le login et le mdp. J'ai importer XMLEncoder et Decoder pour pouvoir sérialiser au format XML, le problème est que ça ne marche pas.
Lorsque j'essaye de désérialiser le fichier, qui est initialement vide, il me donne une
Citation:
org.xml.sax.SAXParseException: Premature end of file.
Je me dis alors que c'est parce que mon fichier est vide (quel logique^^).
Je décide alors de renseigner mon fichier XML comme ceci
Citation:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0_06" class="java.beans.XMLDecoder">
<object class="Compte">
<void property="login">
<string>test</string>
</void>
<void property="mdp">
<string>test</string>
</void>
</object>
</java>
Je pense avoir trouvé la solution mais lorsque que je lui demande à nouveau de charger le fichier, j'ai droit à:
Citation:
java.lang.NullPointerException: target should not be null
Continuing ...
java.lang.NullPointerException: target should not be null
Continuing ...
java.lang.ClassNotFoundException: Compte
Continuing ...
java.lang.NullPointerException: target should not be null
Continuing ...
java.lang.NullPointerException: target should not be null
Continuing ...
java.lang.NullPointerException: target should not be null
Continuing ...
java.lang.NullPointerException: target should not be null
Continuing ...
java.lang.NullPointerException: target should not be null
Continuing ...
liste des comptes
null
Quelqu'un pourrait-il m'expliquer où j'ai merdé?
merci d'avance.
ps: tout conseil pour améliorer mon code et mes compétences est le bienvenu.
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Modeles;
import java.beans.XMLDecoder;
import java.io.FileInputStream;
import java.beans.XMLEncoder;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/**
*
* @author maxiflo
*/
public class Compte implements java.io.Serializable {
private String login;
private String mdp;
private ArrayList listeCompte;
private Compte leCompte;
private boolean auth;
private boolean dispo;
private Compte leCompteASaisir;
private boolean ajout;
public Compte () {
login = null;
mdp = null;
initialize();
}
public Compte (String vlogin){
login = vlogin;
mdp = null;
initialize();
}
public Compte (String vlogin, String vmdp) {
login = vlogin;
mdp = vmdp;
initialize();
}
private void initialize() {
}
//methode qui va vérifier dans le fichier log si le compte existe et si le mdp est correct
public boolean authentification(String vlogin, String vmdp) {
listeCompte = new ArrayList();
listeCompte = loadComptes();
//on recupere les saisies
login = vlogin;
mdp = vmdp;
leCompte = new Compte(login,mdp);
auth = listeCompte.contains(leCompte);
return auth;
}
//methode de recuperation des compte
public ArrayList loadComptes(){
setListeCompte(new ArrayList());
listeCompte = Deserialize("/home/maxiflo/NetBeansProjects/AGES/data/comptes.xml");
System.out.println("liste des comptes");
System.out.println(getListeCompte());
return listeCompte;
}
//methode de verification de disponibilité d'un login
public boolean dispoCompte(String vlogin) {
listeCompte = new ArrayList();
listeCompte = loadComptes();
//on instancie la dispo sur 0
dispo = false;
//on instancie un objet contenant le login
leCompte = new Compte(vlogin);
//on verifie si le login est deja présent
dispo = listeCompte.contains(leCompte);
return dispo;
}
public static void Serialize(ArrayList l){
try {FileOutputStream fos = new FileOutputStream("/home/maxiflo/NetBeansProjects/AGES/data/comptes.xml");
XMLEncoder xenc = new XMLEncoder(fos);
xenc.writeObject(l);
xenc.close();
fos.close();
}
catch (FileNotFoundException e) {// TODO Auto-generated catch block
e.printStackTrace();} catch (IOException e) {
}
}
public ArrayList Deserialize(String vfichier){
// Create input streams.
FileInputStream fis;
String fichier = vfichier;
listeCompte = new ArrayList();
try {
fis = new FileInputStream(fichier);
XMLDecoder xdec = new XMLDecoder(fis);
// Read object.
listeCompte = (ArrayList) xdec.readObject();
xdec.close();
fis.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return listeCompte;
}
//methode pour ajouter un compte
public void ajoutCompte(String vlogin, String vmdp) {
listeCompte = new ArrayList();
listeCompte = loadComptes();
leCompteASaisir = new Compte (vlogin, vmdp);
//on ajoute le nouveau compte dans l'arraylist
ajout = listeCompte.add(leCompteASaisir);
if (ajout == true) {
//on effectue la serialization
Serialize(listeCompte);
}
else {
JOptionPane.showMessageDialog(null, "Echoué", "L'ajout du compte dans l'ArrayList" +
" Veuillez vérifier votre saisie", JOptionPane.ERROR_MESSAGE);
}
}
//-----------------------------------------------------------
// GETTERS ET SETTERS
//-----------------------------------------------------------
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getMdp() {
return mdp;
}
public void setMdp(String mdp) {
this.mdp = mdp;
}
public ArrayList getListeCompte() {
return listeCompte;
}
public void setListeCompte(ArrayList listeCompte) {
this.listeCompte = listeCompte;
}
} |