The Artima Developer Community
Sponsored Link

Java Answers Forum
Passing pimitive types as parameter to a function

4 replies on 1 page. Most recent reply: Jul 7, 2005 3:59 AM by Antonio

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Passing pimitive types as parameter to a function Posted: Dec 2, 2004 1:37 AM
Reply to this message Reply
Advertisement
Hi,

I want to pass primitive type (boolean) as a parameter to a method
schemaObj.getClass().getMethod(methodName, attrClass).invoke(schemaObj, val);

wherein schemaObj - my class
methodName - name of the method to be invoked
attrClass - Class[]
val - Object[].

One of the values in val (Object[]) is of type Boolean (not boolean) and the method methodName has a parameter of type boolean. And the Boolean is not getting converted to boolean. And hence its throwing NoSuchMethodException.

How do I solve this problem.

Regards,
Rajeev


Gunasekhar

Posts: 7
Nickname: dguna
Registered: Nov, 2004

Re: Passing pimitive types as parameter to a function Posted: Dec 2, 2004 4:30 AM
Reply to this message Reply
If you could display the code here, then it would be easy solve.

Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Passing pimitive types as parameter to a function Posted: Dec 2, 2004 4:33 AM
Reply to this message Reply
Hi Rajeev,

For premitive type the value of the Wrapper object of same type is taken. The statement which you have written is correct, check if the method exists with the same signature.

Here is my code which i used to cross check:
package com.amol;
import java.lang.reflect.*;
 
public class RefMethodEx
{
    public void testBool(boolean flag)
    {
        System.out.println("testBool");
    }
    public static void main(String args[])
    {
        try
        {
            RefMethodEx methobj = new RefMethodEx();
            Class cls = Class.forName("com.amol.RefMethodEx");
            Class boolpara[] = new Class[1];
            boolpara[0] = Boolean.TYPE;
            Method boolm = cls.getMethod("testBool", boolpara);
            Object boolarg[] = new Object[1];
            boolarg[0] = new Boolean(true);
            boolm.invoke(methobj, boolarg);
       }
        catch (Throwable e)
        {
            System.err.println(e);
        }
    }
}

Amol Brid

Rajeev Mutalik

Posts: 57
Nickname: rajmutalik
Registered: Sep, 2002

Re: Passing pimitive types as parameter to a function Posted: Dec 3, 2004 2:07 AM
Reply to this message Reply
Hey Amol,

Thanks a lot.

Rajeev

Antonio

Posts: 33
Nickname: arhak
Registered: Jul, 2005

Re: Passing pimitive types as parameter to a function Posted: Jul 7, 2005 3:59 AM
Reply to this message Reply
You use a Boolean Object but you don't say it is of type "Boolean" but Boolean.TYPE, every primitive type has a Class instance associated.
See class Class documentation
Start with the isPrimitive() method

Flat View: This topic has 4 replies on 1 page
Topic: Mouse listener (application) Previous Topic   Next Topic Topic: How to change the sepcified string color in a TextArea or  JTextArea?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use