Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 437 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot cast to array list

#1
I have 2 Objects one called etudiant and second called resultat


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.



Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


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 ?[/hide]
[/hide]
Reply

#2

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


that's the code producing the error.

Quote:(01-19-2017, 07:46 PM)Inori Wrote:

[To see links please register here]

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.[/hide]
Reply

#3
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.
Reply

#4
Quote:(01-19-2017, 07:55 PM)Vi-Sion Wrote:

[To see links please register here]


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


that's the code producing the error.

Quote:(01-19-2017, 07:46 PM)Inori Wrote:

[To see links please register here]

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.[/hide]

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

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


You also probably want to specify the type of object. IE

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


although it is difficult to figure it out if there is no stacktrace/error trace
Reply

#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.
Reply

#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
Reply

#7
@

[To see links please register here]

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 ?
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through