![]() |
|
Cannot cast to array list - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE) +--- Thread: Cannot cast to array list (/Thread-Cannot-cast-to-array-list) |
Cannot cast to array list - Vi-Sion - 01-19-2017 I have 2 Objects one called etudiant and second called resultat Spoiler:Code: package projet;
import java.io.Serializable;
import java.util.Date;
public class Resultat {
private String domaine;
private int note;
private Date date;
private int numeroEssaie;
public Resultat(String domaine,int note,Date date,int numeroEssaie){
this.domaine=domaine;
this.note=note;
this.date=date;
this.numeroEssaie=numeroEssaie;
}
public void setDomaine(String domaine){
this.domaine=domaine;
}
public String getDomaine(){
return this.domaine;
}
public void setNote(){
this.note=note;
}
public int getNote(){
return this.note;
}
public void setDate(Date date){
this.date=date;
}
public Date getDate(){
return this.date;
}
public void setNumeroEssaie(int numeroEssaie){
this.numeroEssaie=numeroEssaie;
}
public int getNumeroEssaie(){
return this.numeroEssaie;
}
}Spoiler:Code: package projet;
import java.io.Serializable;
import java.util.ArrayList;
public class Etudiant implements Serializable {
private String nom;
private String prenom;
private String numeroDossier;
private String password;
private ArrayList<Resultat>resultat;
private static long serialVersionUID = -7685469652645975124L;
public Etudiant(String nom,String prenom,String numeroDossier,String password){
this.nom=nom;
this.prenom=prenom;
this.numeroDossier=numeroDossier;
this.password=password;
}
public Etudiant(String nom,String prenom,String numeroDossier,String password,ArrayList<Resultat>resultat){
this.nom=nom;
this.prenom=prenom;
this.numeroDossier=numeroDossier;
this.password=password;
}
public void setNom(String nom){
this.nom=nom;
}
public String getNom(){
return this.nom;
}
public void setPrenom(String prenom){
this.prenom=prenom;
}
public String getPrenom(){
return this.prenom;
}
public void setNumeroDossier(String numeroDossier){
this.numeroDossier=numeroDossier;
}
public String getNumeroDossier(){
return this.numeroDossier;
}
public void setPassword(String password){
this.password=password;
}
public String getPassword(){
return password;
}
public void setResultat(ArrayList<Resultat> resultat){
this.resultat=resultat;
}
public ArrayList<Resultat> getResultat(){
return this.resultat;
}
public void ajouterResultat(Resultat resultat){
this.resultat.add(resultat);
}
public String toString(){
return this.nom+this.numeroDossier+this.password+this.prenom+this.resultat;
}
}i have some etudiant objects stocked in a text file as objects while trying to read them and stock them in an arraylist i get an error java.lang.ClassCastException: projet.Etudiant cannot be cast to java.util.ArrayList any idea why i can't cast it to arraylist ? RE: Cannot cast to array list - Inori - 01-19-2017 Do you have the code that's producing the error? Some guesses are that you're trying to assign the ArrayList<Etudiant> to a single Etudiant instance, or you're trying to use the shorthand append available in Python (+=) which isn't implemented for ArrayLists. The most I can help without seeing the code is by suggesting that you don't (or at least shouldn't) need to do any casting. If you have some deserialized Etudiant objects, you should just be able to use the ArrayList.add() method in a loop to add them. Just a tip, as well: you're doing a lot of unnecessary encapsulation. Instead of having all those getters and setters, you could just make your instance variables publicly accessible. RE: Cannot cast to array list - Vi-Sion - 01-19-2017 Spoiler:Code: File dir=new File(System.getProperty("user.dir")+"//Etudiant//etudiant.txt");
FileInputStream fin = new FileInputStream(dir);
ObjectInputStream ois = new ObjectInputStream(fin);
ArrayList<Etudiant> test = (ArrayList)ois.readObject();
System.out.println(test.toString());that's the code producing the error. (01-19-2017, 07:46 PM)Inori Wrote: Just a tip, as well: you're doing a lot of unnecessary encapsulation. Instead of having all those getters and setters, you could just make your instance variables publicly accessible. your right but it's a project for uni i'm working on and they asked for those getters and setters. RE: Cannot cast to array list - insidious - 01-20-2017 (01-19-2017, 07:55 PM)Vi-Sion Wrote: What error is it spitting out? It seems you might have to initialize your arraylist, allocate memory on the Heap so that it has somewhere to live. IE Code: ArrayList<Etudiant> list = new ArrayList<Etudiant>(); //(or something like that, haven't done Java in a while)You also probably want to specify the type of object. IE Code: list = (ArrayList<Etudiant>ois.readObject();although it is difficult to figure it out if there is no stacktrace/error trace RE: Cannot cast to array list - DarkMuse - 01-20-2017 Okay so here's my take. Instead of doing whatever the fuck you're doing, serialize an arraylist<etudiant> to the .txt file or a .dat file, then desrialize whenever you need it back into an arraylist. I've used this 16 bajillion times and it's fantastic, just make sure to set the serializedUID to a value. If you need more details lemme know. RE: Cannot cast to array list - Vi-Sion - 01-20-2017 Yep thank you i've done this before but this time i forgot to add it to an arraylist and then save the arraylist in a file . Found this mistake yesterday and fixed it thanks again for all your replies RE: Cannot cast to array list - Vi-Sion - 01-20-2017 @DarkMuse i know i have to set the serializedUID to a value but why ? and do i have to do this everytime i serialize ? any given value will work ? |