Hi again, I know I've asked a lot of questions, and you guys have been a really big help, so sorry for all the trouble. XD But, I've got another problem. :X I've been making a reflective program, and it's more or less working right now except of couple of casting/wrapping problems.


My program keeps crashing whenever I try to run a reflective method using anything other then Strings. When I do a small test using a method that requires integers, I get the following error:

java.lang.NoSuchMethodException: Example.intint(java.lang.Integer, java.lang.Integer)

Below is my test class Example:

public class Example {

	public static void output() {
		System.out.println("Running output");
	}
	
[B]	public int intint(int var1, int var2) {
		return (var1 + var2);[/B]
	}
	public String stringint(String x, int y) {
		System.out.println(x);
		System.out.println(y);
		return null;
	}
	public String stringstring (String x, String y) {
		return x.concat(y);
	}
}

What I don't get is that I have an intint method that requires 2 int variables. How come my program can't seem to catch it? I'm currently using Integer.class; as my way of using intst as the class parameter, and String.class for Strings. Does anyone the proper way I should cast/wrap it?

Recommended Answers

All 6 Replies

hi
instead of :

public int intint(int var1, int var2) {
return (var1 + var2);
}

try this:

public int intint(Integer var1, Integer var2) throws IllegalArgumentException
{

              if(var1==null||var2==null)
              {
                  throw new IllegalArgumentException("Null Values not allowed as parameter");
              }

              int a=var1.intValue();
              int b=var2.intValue();

	 return (a+b);
}

Now you are trying to pass Integer and want it to be converted to int which is throwing an exception.
Try this and tell me.

Regards
Srinivas

After some more study (in the wee hours of morning) I found the solution (I hope, I've not tested it).
Rather than declare the argument as Integer.class define it as int.class (I know it looks strange).

These are special classes used internally by the JVM to represent primitive types.
They have the same name as the primitive types.

OK, it indeed works.
Here's an example:

import java.lang.reflect.*;

public class TestClass {

    public int doSomething(int i) {
        return i;
    }

    public static void main(String args[]) {
        TestClass c = new TestClass();
        try {
            System.out.println(c.getClass().getMethod("doSomething", new Class[] {int.class}).
                               invoke(c, new Object[] {1}));
        } catch (SecurityException ex) {
          ex.printStackTrace();
        } catch (NoSuchMethodException ex) {
          ex.printStackTrace();
        } catch (InvocationTargetException ex) {
          ex.printStackTrace();
        } catch (IllegalArgumentException ex) {
          ex.printStackTrace();
        } catch (IllegalAccessException ex) {
          ex.printStackTrace();
        }

    }
}

Ahhh thanks so much! I can't believe I never tried using int.class (Even if it doesn't make any sense). I punch myself in face.

hi jwenting,

When i tried compiling the program, I got a compiler error.

TestClass.java:13: incompatible types
found   : int
required: java.lang.Object
                               invoke(c, new Object[] {1}));

I modified it like this:

import java.lang.reflect.*;

public class TestClass {

    public int doSomething(Integer i) {
        return i.intValue();
    }

    public static void main(String args[]) {
        TestClass c = new TestClass();
        try {
            System.out.println(c.getClass().getMethod("doSomething", new Class[] {Integer.class}).invoke(c, new Object[] {new Integer(1)}));
        } catch (SecurityException ex) {
          ex.printStackTrace();
        } catch (NoSuchMethodException ex) {
          ex.printStackTrace();
        } catch (InvocationTargetException ex) {
          ex.printStackTrace();
        } catch (IllegalArgumentException ex) {
          ex.printStackTrace();
        } catch (IllegalAccessException ex) {
          ex.printStackTrace();
        }

    }
}

then it started working.
regards
Srinivas

Ah, you must be using a 1.4 compiler.
I'm using 1.5 here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.