时光如梭,转眼间到了2011年,新的一年,新的开始,希望同学们在这一年里有更大的收获. 我们开始了今年的第一个班级授课任务
,这几天都忙于给学员加强java基础.
在忙于授课的同时,把今天学员遇到的第一个关于反射的bug:java.lang.IllegalArgumentException: wrong number of
arguments的问题解析如下:
- 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();
- }
以上内容归redarmy_chen原创,版权归redarmy_chen所有不得随意转载 如有问题请发送邮件到redarmy_chen@qq.com