How to return a 2D array using Reflection?

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer 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

  1. Using reflection I stored all the methods from DataStoragePics class in the methodStorage[].
  2. Then I invoke a method from methodStorage[] which will then store in a tempMatrix[][].
  3. Using a loop and another method (not shown here) I will later use to find out what type of Picture the captured set of pixels are.

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:

Multiple markers at this line - Type mismatch: cannot convert from Object to int.

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 the tempMatrix to be a simple array or the methodStorage[] to be a 2D array. I need help solving that error.

This is the 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 DataStoragePics class:

public class DataStoragePics
{

  public int[][] picXYZ()
    {
      int[][] rgbValues = 
      {
        {1,2,3,4},
        {9,8,7,6}
      };

      return rgbValues;
    }
}

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.

profilepic.png
manpreet 2 years ago

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 hashCodetoString 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;
    }

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.