Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

How to return a 2D array using Reflection?

Course Queries Syllabus Queries

Max. 2000 characters
Replies

usr_profile.png

User

( 3 months 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.

usr_profile.png

User

( 3 months 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;
    }

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif