About calling method with arguments

Hi,
I have a problem about calling method using reflection. my method is like follows:
public myMethod(Integer var1, MyObject mobj) {
I've tried to call the method using the following code,
Class[] parameterTypes = new Class[] {Integer.class, MyObject.class};
Object[] arguments = new Object[] {new Integer(2), mobj};
Method met=cl.getMethod("myMethod", parameterTypes);
But the in the last line NoSuchMethodException is thrown.
How can I send the reference of MyObject to myMethod()?
Thanx
rony

Should work ok:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
     static class MyObject {}
     public static void main(String[] args) throws Exception {
          Class c = Test.class;
          Class[] parameterTypes = new Class[] {Integer.class, MyObject.class};                              
          try {
               Object[] arguments = new Object[] {new Integer(2), new MyObject()};
               Method met = c.getMethod("myMethod", parameterTypes);
               met.invoke(new Test(), arguments);
          } catch (NoSuchMethodException e) {
               System.out.println(e);
          } catch (IllegalAccessException e) {
                 System.out.println(e);
          } catch (InvocationTargetException e) {
               System.out.println(e);
     public void myMethod(Integer var1, MyObject mobj) {
          System.out.println("myMethod");
}

Similar Messages

  • A question about a method with generic bounded type parameter

    Hello everybody,
    Sorry, if I ask a question which seems basic, but
    I'm new to generic types. My problem is about a method
    with a bounded type parameter. Consider the following
    situation:
    abstract class A{     }
    class B extends A{     }
    abstract class C
         public abstract <T extends A>  T  someMethod();
    public class Test extends C
         public <T extends A>  T  someMethod()
              return new B();
    }What I want to do inside the method someMethod in the class Test, is to
    return an instance of the class B.
    Normally, I'm supposed to be able to do that, because an instance of
    B is also an instance of A (because B extends A).
    However I cannot compile this program, and here is the error message:
    Test.java:16: incompatible types
    found   : B
    required: T
                    return new B();
                           ^
    1 errorany idea?
    many thanks,

    Hello again,
    First of all, thank you very much for all the answers. After I posted the comment, I worked on the program
    and I understood that in fact, as spoon_ says the only returned value can be null.
    I'm agree that I asked you a very strange (and a bit stupid) question. Actually, during recent months,
    I have been working with cryptography API Core in Java. I understood that there are classes and
    interfaces for defining keys and key factories specification, such as KeySpec (interface) and
    KeyFactorySpi (abstract class). I wanted to have some experience with these classes in order to
    understand them better. So I created a class implementing the interface KeySpec, following by a
    corresponding Key subclass (with some XOR algorithm that I defined myself) and everything was
    compiled (JDK 1.6) and worked perfectly. Except that, when I wanted to implement a factory spi
    for my classes, I saw for the first time this strange method header:
    protected abstract <T extends KeySpec> T engineGetKeySpec
    (Key key, Class<T> keySpec) throws InvalidKeySpecExceptionThat's why yesterday, I gave you a similar example with the classes A, B, ...
    in order to not to open a complicated security discussion but just to explain the ambiguous
    part for me, that is, the use of T generic parameter.
    The abstract class KeyFactorySpi was defined by Sun Microsystem, in order to give to security
    providers, the possibility to implement cryptography services and algorithms according to a given
    RFC (or whatever technical document). The methods in this class are invoked inside the
    KeyFactory class (If you have installed the JDK sources provided by Sun, You can
    verify this, by looking the source code of the KeyFactory class.) So here the T parameter is a
    key specification, that is, a class that implements the interface KeySpec and this class is often
    defined by the provider and not Sun.
    stefan.schulz wrote:
    >
    If you define the method to return some bound T that extends A, you cannot
    return a B, because T would be declared externally at invocation time.
    The definition of T as is does not make sense at all.>
    He is absolutely right about that, but the problem is, as I said, here we are
    talking about the implementation and not the invocation. The implementation is done
    by the provider whereas the invocation is done by Sun in the class KeyFactory.
    So there are completely separated.
    Therefore I wonder, how a provider can finally impelment this method??
    Besides, dannyyates wrote
    >
    Find whoever wrote the signature and shoot them. Then rewrite their code.
    Actually, before you shoot them, ask them what they were trying to achieve that
    is different from my first suggestion!
    >
    As I said, I didn't choose this method header and I'm completely agree
    with your suggestion, the following method header will do the job very well
    protected abstract KeySpec engineGetKeySpec (Key key, KeySpec key_spec)
    throws InvalidKeySpecException and personally I don't see any interest in using a generic bounded parameter T
    in this method header definition.
    Once agin, thanks a lot for the answers.

  • How do I call methods with the same name from different classes

    I have a user class which needs to make calls to methods with the same name but to ojects of a different type.
    My User class will create an object of type MyDAO or of type RemoteDAO.
    The RemoteDAO class is simply a wrapper class around a MyDAO object type to allow a MyDAO object to be accessed remotely. Note the interface MyInterface which MyDAO must implement cannot throw RemoteExceptions.
    Problem is I have ended up with 2 identical User classes which only differ in the type of object they make calls to, method names and functionality are identical.
    Is there any way I can get around this problem?
    Thanks ... J
    My classes are defined as followes
    interface MyInterface{
         //Does not and CANNOT declare to throw any exceptions
        public String sayHello();
    class MyDAO implements MyInterface{
       public String sayHello(){
              return ("Hello from DAO");
    interface RemoteDAO extends java.rmi.Remote{
         public String sayHello() throws java.rmi.RemoteException;
    class RemoteDAOImpl extends UnicastRemoteObject implements RemoteDAO{
         MyDAO dao = new MyDAO();
        public String sayHello() throws java.rmi.RemoteException{
              return dao.sayHello();
    class User{
       //MyDAO dao = new MyDAO();
       //OR
       RemoteDAO dao = new RemoteDAO();
       public void callDAO(){
              try{
              System.out.println( dao.sayHello() );
              catch( Exception e ){
    }

    >
    That's only a good idea if the semantics of sayHello
    as defined in MyInterface suggest that a
    RemoteException could occur. If not, then you're
    designing the interface to suit the way the
    implementing classes will be written, which smells.
    :-)But in practice you can't make a call which can be handled either remotely or locally without, at some point, dealing with the RemoteException.
    Therefore either RemoteException must be part of the interface or (an this is probably more satisfactory) you don't use the remote interface directly, but MyInterface is implemented by a wrapper class which deals with the exception.

  • Error when calling method with a return of double in j2me

    hello all,
    i have following problem with a j2me program:
    if i call a method with a return of double, then i get following error
    message:
    ERROR: floating-point constants should not appear
    Error preverifying class test.hallo
    what i'm doing wrong
    thanks in regard
    ----------------example----------------------------
    double yourValue(int y, int m, int d)
    double v = 0.10;
    v = 3.39 y m *d
    return v;
    public void startApp()
    int td;
    int y =2;
    int m =2;
    int d =2;
    td = yourValue(y,m,d);
    return(td);

    It's true for MIDP 1.0.
    But you can always use implementation of the float
    point arithmetic which was written by independent
    developers. For example see J2ME section of my
    homepage http://henson.newmail.ru
    anyway, double is reserved word in java, the way you wrote the source code in your example neither the preverifier nor the compiler will recognize that you intend to use your own types for double and float. maybe with Double or Float it would be different ...
    further question: you declare a void function, in the body, however, you try to return some value. something wrong with this function??
    regards
    bernard

  • Calling Method with RFC Destination

    Hi
    This is probably a dumb question, but here goes.  Is it possible to call a method with a RFC destination ?
    Thanks
    Jill

    hi in your method pass the Destination as importing parameter, adn then you can call rfc in side the method by giving destination.
    you can do only this way.
    regards
    vijay

  • Call function with arguments in AS3

    Hello!
    I`m a new in Flex developing, and cannot understand same code
    convention, im Java programmer.
    How I can write correct function call in ActionScript, my
    call: var goodsWnd:CreateGoodsWindow =
    PopUpManager.createPopUp(this,
    CreateGoodsWindow, true) as CreateGoodsWindow;
    I wish call function above with argument, how I do that?
    Where my class: public class CreateGoodsWindow extends
    extends TitleWindow
    public CreateGoodsWindow(data:Object)
    }

    Use PopUpManager.addPopUp() instead of createPopUp().
    addPopUp takes an object that has already been instantiated:
    var createGoodsWindow:CreateGoodsWindow = new
    CreateGoodsWindow(data);
    PopUpManager.addPopUp(createGoodsWindow);

  • About call method

    hi, i want to open a new window when i presses the button, so , should the new window also be a class or just a component.

    en, hehe,firstly, i am rookie in Java, i dont know lots about it.
    what i want to do is :
    if else(e.getSource()=j1){
    display();
    } ///j1 is one instance of JButton
    so ,that's question. here, can i create a new class called display so i can design the panel or just call method display(), does that make sense to you, thx

  • Call method with an argument from another view controller

    I have a UIViewController MainViewController that brings up a modal view that is of the class AddPlayerViewController. When the user clicks 'Save' in the modal view I need to pass the Player data (which is a Player class) from the modal view to the MainViewController in addition to triggering a method in the MainViewController. What's the best way to accomplish this? I'm new to cocoa and have only tried using delegates and some ugly hacks to no avail.
    Thanks for the help.

    If I understand correctly, you have:
    1. A model object, Player.
    2. A top view controller, MainViewController.
    3. Another view controller, AddPlayerViewController, which MainViewController displays modally.
    I'm guessing that AddPlayerViewController creates a new Player object and lets the user set its values, and you need a way to get that new Player into MainViewController once they're done.
    So, here's what I'd do:
    1. Create an AddPlayerViewControllerDelegate protocol. It should declare two methods, "- (void)addPlayerViewController:(AddPlayerViewContrller*)controller didAddPlayer:(Player*)newPlayer" and "- (void)addPlayerViewControllerNotAddingPlayer:(AddPlayerViewController*)controll er".
    2. Add an attribute of type "id <AddPlayerViewControllerDelegate>" called delegate to AddPlayerViewController. Also add a property with "@property (assign)" and "@synthesize".
    3. Modify AddPlayerViewController so that if you click the "Save" button, addPlayerViewController:didAddPlayer: gets called, passing "self" and the new Player object as the two arguments. Also arrange for clicking the "Cancel" button to call addPlayerViewControllerNotAddingPlayer:.
    4. Modify MainViewController to declare that it conforms to AddPlayerViewControllerDelegate. Implement those two methods (addPlayerViewControllerNotAddingPlayer: might be an empty method if you don't want to do anything).
    5. When you create your AddPlayerViewController, set its delegate to your MainViewController.
    If you need more detail, let me know what parts you need me to elaborate on.

  • Call applet method with arguments from JavaScript

    I have an applet with a method doSomething(int a, String b, double c), I want get a,b,c from threee text fields put into an HTML page, and pass them to the applet method. I've a very poor knowledge in JavaScript, how can I pass correctly the arguments without raise a CastException? It seems to me that JavaScript doesn't have int, String, double.....

    Hi!
    You can verify before you send the parameters to the applet
    Something like that:
    var result = parseInt (a);
    if (result =NaN)
    alert("Sorry not Int");
    return false;
    and after that you can send the parammeters to the applet that way:
    document.yourApplet.Your_function_from_applet(result,..)
    hope this help!
    adina

  • (JavaScript, CS3) calling functions with arguments on click?

    Hi all,
    this is getting tricky:
    I want to call a function when the user clicks on a button in my scripted application (a javascript window dialog).
    Unfortunately, I need to pass several arguments to the function.
    According to the scripting documentation, ".onClick" functions won't take arguments.
    I would (somewhat reluctantly) work with (global) variables but the function itself is limited to variables within its own scope - blocking all variables set in main()
    How should I approach this?
    The basic idea is to have people set some settings in the UI and then do some things after they click ok.
    The actual work is pretty complicated (replacing colors etc) and needs to be done to a lot of objects so it would make sense to do it in a function.
    I'm pretty confused right now and don't know how to proceed.
    Any hints are appreciated.
    Many thanks,
    Mike

    It took some tinkering but I got it to work.
    But I still don't see how I can access the whole DOM from within the onClick functions:
    I can get to the dialog properties (this.parent.parent) but nevertheless don't have any way to access app.documents and such.
    I was able to manage my way around by making the whole window a dialog instead of a window - thereby getting a return value from the OK button and being able to run the code from within main().
    Still strange, though...
    Cheers,
    Mike

  • Problem in calling method with object in another class

    Hi All,
    Please tell me the solution, I have problem in calling a method I am posting the code also
    First Program:-
    import java.io.*;
    public class One
    public One()
    System.out.println("One:Object created");
    public void display()
    System.out.println("One:executing the display method");
    static
    System.out.println("One:executing the static block");
    Second Program:-
    import java.io.*;
    public class Two
    public Two()
    System.out.println("Two:Object created");
    public static void main(String arg[])throws Exception
    System.out.println("Two:executing the main method");
    System.out.println("Two:loading the class and creating the object::One");
    Object o=Class.forName("One").newInstance();
    System.out.println(o);
    o.display(); //displaying error here in compile time.
    static
    System.out.println("Two:executing the static block");
    waiting for your answer,
    thanks in advance,bye.

    Hi All,
    Please tell me the solution, I have problem in
    calling a method I am posting the code also
    First Program:-
    import java.io.*;
    public class One
    public One()
    System.out.println("One:Object created");
    public void display()
    System.out.println("One:executing the display
    method");
    static
    System.out.println("One:executing the static
    block");
    Second Program:-
    import java.io.*;
    public class Two
    public Two()
    System.out.println("Two:Object created");
    public static void main(String arg[])throws
    Exception
    System.out.println("Two:executing the main
    method");
    System.out.println("Two:loading the class and
    creating the object::One");
    Object o=Class.forName("One").newInstance();
    System.out.println(o);
    o.display(); //displaying error here in compile
    time.
    static
    System.out.println("Two:executing the static
    block");
    waiting for your answer,
    hanks in advance,bye.the line
    o.display()
    could be written as
    ((One)o).display();

  • About communication method with integration server?

    can anybody explain clearly what difference between using proxy and adapters at runtime?
    especially for adatpers,i am confused.
    there is a the following explanation in online help.
    Communication using adapters. In this case, you create interfaces for message exchange in the application system, or use existing interfaces.
    does not adapter have proxy programe? if so,where is its implementation?

    Hi,
    Adapter and proxies both are used by the appication to communicate with the XI/PI.
    Adapters: "To adapts the things", any appication(SAP, non-SAP) can communicate with XI using Adapters. what adpaters do? It will convert the source
    message format to target message format. Example SAP send the Idoc but XI only understand XML message so no communication is possible till some one come in bewteen and traslate that Idoc to XML format..this is the task of adatpers.
    Except Idoc and HTTP all adapters reside in the Java stack.
    Proxies are also used for the same purpose but they directly communicate with the IS so they are always fasters.
    There can be ABAP proxy or Java proxy(Client or Server Proxy). Example: if you have J2EE appication and that need to communicate with XI, for that no adapter is available...then you can write Client Java Proxy which can communicate with XI directly(same for Server proxy....where XI will call the J2EE application)
    Proxies need to use if you are communicating with SAP systems.even Idoc and RFC adapters are available? Then it depends if Web Appication Server >= 6.20 then its always to good to use proxies(even you have an option for RFC or Idoc adapters).
    Thanks & Regards,
    Farooq Farooqui.
    **Rewards Points if you find it useful**

  • Problem with f:invoke - cannot find method with argument

    Hi,
    I have <f:invoke var="${bpmObj}" methodName="tempMethod" retAttName="... /> in my JSP.
    Now, in ALBPM I have this method tempMethod and I gave it one out argument so it can return something to my JSP variable given in retAttName.
    Problem is that it cannot find this method because it is something like tempMethod(out resultString) and my JSP invoke has no input/output attributes at all.
    It is working only when I delete all in/out args from definition of my method, but I want it to return something! How to do it? please help.
    regards,
    Paul

    Hi.
    Search under sap portal->admi->support->ROOT/WEB-INF/deployment/pcd/ for com.sap.km.cm.repository.service.base.par.bak
    Best regards,
    Aliaksandr Zhukau

  • Calling MSBuil with arguments in PowerShellScript

    Hi,
    I am trying to call below line of code from powershell script. But it is giving an error..Installer.target file exists in the same location (C:\Projects\Main\src>).
    What is the wrong with below code? Could you please advise.
    PS C:\Projects\Main\src> "C:\Program Files (x86)\MSB
    uild\12.0\Bin\MSBuild.exe" installer.targets /target:Installer /p:Version=1.0.8.
    0"
    Unexpected token 'installer.targets' in expression or statement.
    At line:1 char:72
    + "C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe" installer.targets <<<<
     /target:Installer /p:Version=1.0.8.0"
        + CategoryInfo          : ParserError: (installer.targets:String) [], Pare
       ntContainsErrorRecordException
        + FullyQualifiedErrorId : UnexpectedToken
    function Execute-Installar
    Param ([string]$CustomBuildPath)
    LogWrite "Creating msi : "
    $msbuild ="${env:ProgramFiles(x86)}\MSBuild\12.0\Bin\MSBuild.exe"
    $Args = " installer.targets" + " /target:Installer" + " /p:Version=1.0.8.0"
    Write-Host $msbuild
    Write-Host $Args
    $build="""$msbuild""" + $Args
    Write-Host $build
    Invoke-Expression $build

    Hi,
    well, the first thing that is wrong is how you are trying to launch an application. Try this instead:
    Start-Process $msbuild -ArgumentList @("installer.targets", "/target:Installer", "/p:Version=1.0.8.0")
    Cheers,
    Fred
    There's no place like 127.0.0.1

  • About:call procedure with returned resultset

    how can i do for that?
    thanks!

    CREATE OR REPLACE FUNCTION selectAllEmployments
    RETURN SYS_REFCURSOR
    AS
    st_cursor SYS_REFCURSOR;
    BEGIN
    OPEN st_cursor FOR
    SELECT EMPLOYEE, EMPLOYER,
    STARTDATE, ENDDATE,
    REGIONCODE, EID, VALUE, CURRENCY
    FROM EMPLOYMENT;
    RETURN st_cursor;
    END;
    To use this query in Hibernate you need to map it via a named query.
    <sql-query name="selectAllEmployees_SP" callable="true">
    <return alias="emp" class="Employment">
    <return-property name="employee" column="EMPLOYEE"/>
    <return-property name="employer" column="EMPLOYER"/>
    <return-property name="startDate" column="STARTDATE"/>
    <return-property name="endDate" column="ENDDATE"/>
    <return-property name="regionCode" column="REGIONCODE"/>
    <return-property name="id" column="EID"/>
    <return-property name="salary">
    <return-column name="VALUE"/>
    <return-column name="CURRENCY"/>
    </return-property>
    </return>
    { ? = call selectAllEmployments() }
    </sql-query>

Maybe you are looking for

  • Internet connection keeps dropping at night

    I'm having a bit of a weird problem with my internet connection. I connect via an ethernet cable and the connection keeps dropping. It generally starts after 7pm and seems to be every 40 minutes. I have changed the microfilters and ethernet cable but

  • Upgrading to a clone on an external drive

    I want to try out Mavericks. I am currently running Snow Leopard. I was thinking about cloning my internal drive to an exteral firewire drive. Can I use the clone to install Mavericks and test it? How exactly would that work? Do I boot on the cloned

  • I am trying to update my ipod touch.But ios6 will not down load

    i have an ipod touch 4 version 4.2.1 that i am trying to update to 6.1.3 How ever every time i try downloading it through itunes the download stops and after about 3 minutes i get a box saying my network has timed out. I have run the trouble shooteer

  • Safari not filling in web addresses automatically like it used to

    Used to be able to type in something like 'earthlink' into the url address and Safari would automatically fill it in as 'earthlink.com' but now it is not doing this. Not aware of any changes to preferences, etc. Any ideas?

  • Quick Time Pro to TV Safe

    Hi. Does anybody know how can I resize my QT Pro to a TV Safe size. At the moment I used iMovie and Burnt my DVD but when played on TV I can not see the edges of my slides. I have clicked the Resize option in iMovie but still the images are too large