首页

对象因构成函数问题报"java.lang.IllegalAccessException"、"java.lang.InstantiationException"及“java.lang.IllegalArgumentException”三种不同异常

标签:IllegalAccessException,构成函数异常,exception,InstantiationException,IllegalArgumentException     发布时间:2018-06-16   

一、异常描述

对象工具类定义内部类,并在Map转换对象时候因实例化问题报"java.lang.IllegalAccessException: Can not call newInstance() on the Class for java.lang.Class"、"java.lang.InstantiationException: test.ObjectReflectUtils$Student"、"java.lang.IllegalArgumentException: wrong number of arguments"异常问题,详情日志如下

java.lang.IllegalAccessException: Can not call newInstance() on the Class for java.lang.Class@b@	at java.lang.Class.newInstance0(Class.java:322)@b@	at java.lang.Class.newInstance(Class.java:310)@b@	at test.ObjectReflectUtils.mapToObj(ObjectReflectUtils.java:117)@b@	at test.ObjectReflectUtils.main(ObjectReflectUtils.java:229)@b@Exception in thread "main" java.lang.NullPointerException@b@	at test.ObjectReflectUtils.main(ObjectReflectUtils.java:231)@b@..@b@java.lang.InstantiationException: test.ObjectReflectUtils$Student@b@小木人@b@	at java.lang.Class.newInstance0(Class.java:342)@b@	at java.lang.Class.newInstance(Class.java:310)@b@	at test.ObjectReflectUtils.mapToObj(ObjectReflectUtils.java:118)@b@	at test.ObjectReflectUtils.main(ObjectReflectUtils.java:229)@b@Exception in thread "main" java.lang.NullPointerException@b@	at test.ObjectReflectUtils.main(ObjectReflectUtils.java:231)@b@..	@b@java.lang.IllegalArgumentException: wrong number of arguments@b@	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)@b@	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)@b@	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)@b@	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)@b@	at test.ObjectReflectUtils.mapToObj(ObjectReflectUtils.java:119)@b@	at test.ObjectReflectUtils.main(ObjectReflectUtils.java:230)@b@Exception in thread "main" java.lang.NullPointerException@b@	at test.ObjectReflectUtils.main(ObjectReflectUtils.java:232)

二、解决方法

1. 如下代码示例ObjectReflectUtils.Student内部类(完整代码示例参见其他文章),在mapToObj方法cla.newInstance()构成初始化

public class ObjectReflectUtils {@b@	@b@	public  static  class  Student{@b@		@b@		private  String  name;@b@		private  String  classId;@b@		 @b@		public Student(String name, String classId) {@b@			super();@b@			this.name = name;@b@			this.classId = classId;@b@		}@b@		.. @b@	public static Object mapToObj(Map map, Class cla) {@b@		if (cla == null) {@b@			return null;@b@		}@b@		Object obj = null;@b@		try {@b@//			obj = Class.forName(cla.getClass().getName()).newInstance();@b@			obj = cla.newInstance();// 创建 JavaBean 对象@b@//			obj =cla.getConstructors()[0].newInstance();@b@		} catch (Exception e1) {@b@			e1.printStackTrace();@b@			return null;@b@		}@b@		try {@b@			// 获取属性@b@			BeanInfo beanInfo = Introspector.getBeanInfo(cla);@b@			// 给 JavaBean 对象的属性赋值@b@			putValueToJavaBean(map, obj, beanInfo);@b@		} catch (Exception e) {@b@			e.printStackTrace();@b@		}@b@		return obj;@b@	}	@b@	..@b@	public static void main(String[] args)  throws  Exception{@b@		@b@		Map printMap=new HashMap();@b@		printMap.put("classId", "c01");@b@		printMap.put("name", "小木人");@b@		@b@		Student stu2=(Student)mapToObj(printMap,ObjectReflectUtils.Student.class);@b@		System.out.println("mapToOb:"+stu2.toString());@b@		@b@	}@b@}

2.从上面代码示例的cla.newInstance()构成初始化时,因为内部类public Student()构成函数造成IllegalAccessException异常,修改如下

public class ObjectReflectUtils {@b@	@b@	public  static  class  Student{@b@		@b@		private  String  name;@b@		private  String  classId;@b@		@b@		public Student() {@b@			super();@b@		}@b@		public Student(String name, String classId) {@b@			super();@b@			this.name = name;@b@			this.classId = classId;@b@		}@b@		.. @b@	 @b@}

3.运行问题解决