How do I return a 2D array using Reflection?
Purpose of the code
To find out which picture is on the screen by comparing it to a pre-existing set of pixels, which are in the form of methods that return 2D arrays in the class DataStoragePics
.
How I tried to solve it
- Using reflection, I saved all the methods from the class
DataStoragePics
inmethodStorage[]
. - Then I call the method from
methodStorage[]
, which will then be stored intempMatrix[][]
. - Using a loop and another method (not shown here) I will later use to find out what type of image the captured pixel set has.
I need help with
When I try to solve the problem using the steps above, I get an error on the third line from the bottom in the main class, repeated twice:
Multiple markers on this line - type mismatch: cannot convert from Object to int.
I think the problem methodStorage[x].invoke(DataStoragePicsObj)
is the only array, but it returns a 2D array and the program does not recognize that it needs tempMatrix
either a simple array or a methodStorage[]
2D array for that. I need help solving this error.
This is a Main
Class:
import java.lang.reflect.Method;
int [][] tempMatrix = new int[16][450];
//Creates a DataStoragePics Object.
DataStoragePics DataStoragePicsObj = new DataStoragePics();
//Stores all DataStoragePics methods in methods[].
Method[] methodStorage = DataStoragePicsObj.getClass().getMethods();
//Loops through methodStorage[].
for(int x = 0; x < method.length; x++)
{
//Stores a 2D array from DataStoragePics class in tempMatrix.
//All methods in DataStoragePics return a 2D array with [16][10] dimensions.
/*This is the error line*/
tempMatrix[16][10] = methodStorage[x].invoke(DataStoragePicsObj);
/*above is the error line*/
}
This is part of the class DataStoragePics
:
public class DataStoragePics
{
public int[][] picXYZ()
{
int[][] rgbValues =
{
{1,2,3,4},
{9,8,7,6}
};
return rgbValues;
}
}
I'm getting a little bit of a start when it comes to java / encoding, so please don't use complicated terms.
Syllabus's answer helped, but I still get this error: "Exception in thread" main "java.lang.ClassCastException: java.lang.Class cannot be thrown in [[I" Thing is thrown and stores and returns stuff to screen. Sometimes it sometimes shows an error at the end, sometimes in the middle. I do not know why.
source to share
You are iterating over the all methods in your class. And casting the return value of each of these methods to int[][]
.
Method[] methodStorage = DataStoragePicsObj.getClass().getMethods();
//Loops through methodStorage[].
for(int x = 0; x < method.length; x++)
This will, of course, fail because your DataStoragePicsObj
class is implicitly extending java.lang.Object
that has methods like hashCode
, toString
and getClass
that don't return int[][]
.
If you call a method through reflection, you must be prepared to pass the correct arguments and return its value; if you can't, you shouldn't call the method through reflection.
What you can do - if my understanding of what you want to do is correct - is to check the return type as well as the arguments to make sure you are ready to handle the reflexive call:
Method m = methodStorage[x];
if (m.getReturnType() != int[][].class || m.getParameterTypes().length != 0) {
// Skip this method because it requires arguments or doesn't return int[][]
continue;
}
source to share
Actually, without the "cast", the compiler just thinks what it invoke
returns Object
.
tempMatrix[16][10]
represents element (16, 10) in your array tempMatrix
, so its type is int
.
So assigning an int object raises a compiler error: types before a after "=" must be "the same".
You must first discard the object returned by the call to int[][]
. Then you have to assign an int object [] [] (full tempMatrix?)
tempMatrix = (int[][]) methodStorage[x].invoke(DataStoragePicsObj);
source to share