RE: Cannot cast to array list 01-20-2017, 12:06 AM
#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]](http://i.imgur.com/pBD38Xq.png)
Email: insidious@protonmail.ch





![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)