troubleshooting Question

Class cast problem

Avatar of allelopath
allelopath asked on
Java
5 Comments1 Solution350 ViewsLast Modified:
In the code below, the last line is giving a compile error, saying that it cannot from Object[] to Class[]
Class[] tempTaskClasses = Reflection.getPackageMembers("com.mycompany.mypackage");
List<Class> tempList = Arrays.asList(tempTaskClasses);
List<Class> tempOutList = new ArrayList<Class>();
    for (Class myClass : tempList) {
 		tempOutList.add(myClass);
    }
 	
}
Class[] taskClasses =  tempOutList.toArray();
This fixes it:
Class[] taskClasses =  (Class[]) tempOutList.toArray();
but I don't understand why this is necessary. Even so, it gives a runtime error:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Class;
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros