Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help solving dynamic method invocation exception.

 
anitha nair
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai forum,

Your previous responses on this topic 'method invocation' has greatly helped me in writing a code to invoke method dynamically.As some experts pointed out,the parameters had to be type casted.

Please help me out of my latest trouble.I invoke only a particular method 'methodName' of a particular calss instance 'instance'.But when i run the code Exceptions are thrown for all the methods of that class whereas i require exceptions connected with methodName only.

I just could not figure out my mistake, so please help me out.

Thank you.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please be more clear: What exceptions are thrown (please copy & paste the error messages you get), and how does this differ from what you expect?
 
anitha nair
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai all,let me post the exceptions i get, so that my problem is more clear to you all.

Here i am selecting a class named Emp.class and selecting its first method "setID(int)". I am giving the parameter as 10.But i am getting the following exception.

Apart from the first 3 lines,i suppose, the rest of the exceptions are for the other methods in the class.


java.lang.ArrayIndexOutOfBoundsException: 0
java.lang.ClassCastException: java.lang.Integer
Invoked result: null
Do not support the data type
java.lang.IllegalArgumentException: wrong number of arguments
Name: null
ID: 0
Designation: null
Invoked result: null
Enter name:
Process exited.

thank you
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you call setParameters ?
 
anitha nair
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the Emp.class iam calling.This might help you.I am calling only the first method in my previous code.

Thank you and regards.

 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anitha,
I asked "How did you call setParameters ?"
Please send us the code where you are calling this method.
 
anitha nair
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep.I am sorry i couldnt get you then.Heres the code where i get the method.It is quite a lengthier one.I choose JFileChooser to select the class file,upon which all the methods in that file will be displayed dynamically in a panel as radiobuttons.

The method is selected when i click a radio button.
Hope you will understand my code.Since iam a novice it might seems clumsy to you.

THE FIRST ONE IS THE CODE TO CHOOSE A CLASS FILE AND DISPLAY METHODS.It also selects a method using radio button, action of whose is given below in class MethodRadioActionListener.


AND HERES THE CODE WHICH WORKS UPON SELECTING THE METHOD UPON CLICKING A RADIOBUTTON .IT DISPLAYS TEXTFIELDS FOR USER OUTPUT.
 
anitha nair
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai forum,

I tried various methods.The outputs are vague.the stackTrace is showing error in invocation,

Object result=method.invoke(instance,parameters);

I really dont understand what could be wrong with this.Please,if any one could trace it out do help me.

regards.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first of all, think about what you're doing. You're calling a method that takes an "int" parameter. But your MethodInvoke.actionPerformed() method doesn't work with "int" parameters; it only works with Integer, String, Boolean, and a few other wrapper classes. For an int parameter the argument class will be Integer.TYPE, a special Class object that represents the primitive type int.

My advice to you is to start over completely from scratch. First, read up on the reflection API and study some samples. Don't try writing code until you understand how the API works.

Then start to write just the reflection code, without having it all tangled up in GUI code. Write a little bit, test that little bit, fix it, until it works. Then write a little more, test that bit, fix it, until it works. This is not the way to code -- writing a whole big pile that you barely understand and then trying to figure out what's wrong!

Start by writing a three-line program with a main() in which you call a specific method that takes an int parameter using reflection. Make sure that works. Then try a slightly longer one that takes the name of a method on the command line, but assumes there are no arguments. Then one that assumes there is one argument, and work out all the different possible argument types. Then add a loop so you can handle any number of arguments.

Then think about adding a GUI.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic