cannot convert from Class<capture#1-of ?> to Class<?>[]

What does it mean, the following error message:

Type mismatch: cannot convert from Class<capture#1-of ?> to Class<?>[]

The code is following:

            Class<?>[] encoderClass;
            Class<?>[] encoderClasses = new Class<?>[] {
                Encoder1.class,
                Encoder2.class,
                Encoder3.class
            };

            for(int i=0; i<encoderClasses.length; ++i) {
                encoderClass = encoderClasses[i]; // <------ error is here
            }
Last updated:11/5/2014 3:08:12 AM

1 Answers

Anonymous User
Anonymous User

You're attempting to assign a single class element to a class array (different types as per the error message). Instead, assign each individual element in the for loop

encoderClass[i] = encoderClasses[i];

ensuring that the array is initialized to avoid NPE in the assignment

Class<?>[] encoderClass = new Class<?>[3];

For simplicity you could replace the entire loop with

encoderClass = Arrays.copyOf(encoderClasses,  encoderClasses.length);

Answer