`

原创:Java反射java.lang.IllegalArgumentException: wrong number of arguments错误解析

阅读更多
 时光如梭,转眼间到了2011年,新的一年,新的开始,希望同学们在这一年里有更大的收获. 我们开始了今年的第一个班级授课任务

,这几天都忙于给学员加强java基础.

    在忙于授课的同时,把今天学员遇到的第一个关于反射的bug:java.lang.IllegalArgumentException: wrong number of 

arguments的问题解析如下:

1、错误bug
java.lang.IllegalArgumentException: wrong number of arguments
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at cn.csdn.reflect.StudentTest.test4(StudentTest.java:84)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
	at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

异常引起的原因分析:

引起错误的原码:
public Student(String strs[]){
		System.out.println(strs.length);
	}

执行代码:
   //解析:public cn.csdn.reflect.Student(java.lang.String[])
	@Test
	public void test4()throws Exception{
		//1、加载类
		Class cls = Class.forName("cn.csdn.reflect.Student");
		//2、根据构造器参数类型获取相应的构造器对象    
		Constructor csr = cls.getConstructor(String[].class);
		//3、创建实体对象
		Student entity = (Student)csr.newInstance(new String[]{});
		//4、调用方法
		entity.study();
	}
编译环境:jdk1.6

错误分析:由于Student(String strs[])构造器只有一个String数组的参数所以这是编译器会把字符串数组当作一个可变长度参数传

给对象strs,而我们取得方法只有一个参数,所以就会出现wrong number of arguments的异常,我们只要把字符串数组强制转换为一

个Object对象就可以解决这个异常了,

解决方案:
  //解析:public cn.csdn.reflect.Student(java.lang.String[])
	@Test
	public void test4()throws Exception{
		//1、加载类
		Class cls = Class.forName("cn.csdn.reflect.Student");
		//2、根据构造器参数类型获取相应的构造器对象    
		Constructor csr = cls.getConstructor(String[].class);
		
		String str[]={"111","123"};
		//3、创建实体对象
		Student entity = (Student)csr.newInstance((Object)str);
		//4、调用方法
		entity.study();
	}
分享到:
评论
2 楼 ljc19901124 2012-11-28  
整合到一个参数 也报错,也没解决问题

public BaoYang(int zichanid,String[]str){
this.id = zichanid;
this.baoyangren = str[0];
this.baoyangneirong = str[1];
}


                //取得此实例类所有的构造函数
Constructor<?> cons[] = demo4.getConstructors();

String str[] = {"baoyangren","baoyangneirongs"};


b3 = (BaoYang)cons[3].newInstance(123,(Object)str);


这样也不行,按理说 可以的啊,是哪还没注意到呢,麻烦老师看看
1 楼 ljc19901124 2012-11-28  
老师:你好,我刚自学反射,对于这个报错,我遇到的情况有的不同,帮忙看看
public BaoYang(int zichanid,String baoyangren,String  baoyangneirong){
this.id = zichanid;
this.baoyangren = baoyangren;
this.baoyangneirong = baoyangneirong;
}

           BaoYang b3 =null;
//取得此实例类所有的构造函数
          Constructor<?> cons[] = demo4.getConstructors();

try {
b0 = (BaoYang)cons[0].newInstance();
b1 = (BaoYang)cons[1].newInstance("baoyangxinxiid");
b2 = (BaoYang)cons[2].newInstance("baoyangxinxiid",234);
b3 = (BaoYang)cons[3].newInstance(123,"baoyangren","baoyangneirongs");
       } catch (Exception e2) {
  // TODO Auto-generated catch block
  e2.printStackTrace();
}


System.out.println(b0);//无参没问题
System.out.println(b1);//一个String没问题
System.out.println(b2);//一个String,一个int没问题
System.out.println(b3);//当遇到两个String参数在一起的时候,就不行了

即使像你说的 转换成Object :b3 = (BaoYang)cons[3].newInstance(123,(Object)"baoyangren",(Object)"baoyangneirongs");
这样也不行,报错的原因我明白,但不知道这种情况的改怎么解决,难道的先整合到一个参数String strs[]),试试

相关推荐

Global site tag (gtag.js) - Google Analytics