Class Cast Exception I cant fix

Hey, i’m probably just doing something stupid, but I am unable to resolve my problem by googling and I’m hoping you guys can help if you have the solution on hand.

The error is thrown here,

new Mesh((Transform[])vertices.toArray(),(Quad[])quads.toArray(),(Tri[])tris.toArray())

The error is,

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcore.Transform;
	at renderable.Mesh.fromFile(Mesh.java:136)
	at testing.Main.main(Main.java:78)

“vertices”, “quads” and “tris” are ArrayList’s of their respective types.

Are your lists typified?


//must specify <Transform>
vertices = new ArrayList<Transform>();
...

Then you actually won’t need the casts, thanks to ArrayList being generic.

EDIT: hmm, seems like cast is still necessary, my bad, and that code of yours still runs fine, put a print in a finally block and see what just attempted to cast, my guess is some other object got into your list.

Do that in the following way:

new Mesh(vertices.toArray( new Transform[ vertices.size() ]),
                        quads.toArray( new Quad[ quads.size() ]), 
                        tris.toArray( new Tri[ tris.size() ]))

If, as you seem to suggest “vertices” is an ArrayList or something like that, then the toArray function you call returns an array of Vertice(or whatever that type is), which is not an array of Transform (i.e. you cannot cast Transform[] to Vertice[]). Hence the class cast exception, which is what I would assume to be the problem.

Edit: can you clarify what you ate trying to do with the Transform?

Maybe [icode]vertices[/icode] is of type [icode]ArrayList[/icode]

If that is the case, you should change your code to


new Mesh((Vertex[])vertices.toArray(),(Quad[])quads.toArray(),(Tri[])tris.toArray())

Maybe this should solve your problem.

I think [icode]vertices[/icode] are [icode]indices[/icode] in the code. There is no point in passing vertices along with Quads and Triangles.

Hey thanks for all the help.
Stranger got it (+1).

There is, Mesh keeps track of the vertices of the mesh.

Yes they are :

		ArrayList<Transform> vertices = new ArrayList<Transform>();
		ArrayList<Quad> quads = new ArrayList<Quad>();
		ArrayList<Tri> tris = new ArrayList<Tri>();

Sorry for my late reply, and thanks again.