File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes help solving dynamic method invocation exception. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "help solving dynamic method invocation exception." Watch "help solving dynamic method invocation exception." New topic
Author

help solving dynamic method invocation exception.

anitha nair
Greenhorn

Joined: Jul 12, 2006
Posts: 12
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
anitha nair
Greenhorn

Joined: Jul 12, 2006
Posts: 12
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

How did you call setParameters ?


[My Blog]
All roads lead to JavaRanch
anitha nair
Greenhorn

Joined: Jul 12, 2006
Posts: 12
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

anitha,
I asked "How did you call setParameters ?"
Please send us the code where you are calling this method.
anitha nair
Greenhorn

Joined: Jul 12, 2006
Posts: 12
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

Joined: Jul 12, 2006
Posts: 12
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help solving dynamic method invocation exception.
 
Similar Threads
equlas()
Linux SWING Calculator is Small GUI bad
dynamic method invocation
Reflection Problem
Please help with sorting multiple columns