Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Cannot cast to array list filter_list
Author
Message
Cannot cast to array list #1
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 ?


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply

RE: Cannot cast to array list #2
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.
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

Reply

RE: Cannot cast to array list #3
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.


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply

RE: Cannot cast to array list #4
(01-19-2017, 07:55 PM)Vi-Sion Wrote:
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.

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
(This post was last modified: 01-20-2017, 12:13 AM by insidious.)
[Image: pBD38Xq.png]
Email: insidious@protonmail.ch

Reply

RE: Cannot cast to array list #5
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.
Scientia potentia est

[Image: inkexplosion.jpg]

Reply

RE: Cannot cast to array list #6
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


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply

RE: Cannot cast to array list #7
@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 ?


Everyone should learn how to code, it teaches you how to think.

You don't have to be a Genius to know how to code you just need to be determined.

The computer is incredibly fast, accurate, and stupid; man is unbelievably slow, inaccurate, and brilliant; together they are powerful beyond imagination.

Reply







Users browsing this thread: 1 Guest(s)