CANNOT CONVERT FROM CLASS<CAPTURE#1-OF ?> TO CLASS<?>[]

marcel ethan

Total Post:105

Points:735
Posted by  marcel ethan
 1190  View(s)
Ratings:
Rate this:

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
            }
  1. Pawan Shukla

    Post:29

    Points:203
    Re: cannot convert from Class<capture#1-of ?> to Class<?>[]

    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

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!