You are looping through all the 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 is of course going to fail, because your DataStoragePicsObj
class implicitly extends java.lang.Object
which has methods like hashCode
, toString
and getClass
that do not return int[][]
.
If you invoke a method through reflection, you should be prepared to pass it the right arguments and the process it's return value; if you cannot, you shouldn't invoke the method through reflection.
What you could do - if my understanding of what you want to do is correct - is to check the return type and also the arguments to make sure that you are prepared to handle the reflective invocation:
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;
}
manpreet
Best Answer
2 years ago
Purpose of Code
To find out what picture is on the screen by comparing it with an already existing set of pixels, which are in the form of methods that return 2D arrays, in the
DataStoragePics
Class.How I tried to Solve it
DataStoragePics
class in themethodStorage[]
.methodStorage[]
which will then store in atempMatrix[][]
.What I need help with
When I try to solve the problem by using the steps mentioned above I get an error on the third line from the bottom in the main class repeated twice:
I think the problem is
methodStorage[x].invoke(DataStoragePicsObj)
is a single array but it return a 2D array and the program doesn't recognize that so it needs either thetempMatrix
to be a simple array or themethodStorage[]
to be a 2D array. I need help solving that error.This is the
Main
Class:This is Part of the
DataStoragePics
class:I am bit of a beginner when it come to java/coding so please don't use complicated terms.
Syllabus's answer helped but I am still getting this error: "Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to [[I" The thing is casting and it stores and returns stuff on the screen. Sometimes it shows the error at the end sometimes in the middle. Don't know why.