BeanUtils java.lang.reflect.InvocationTargetException

最近在使用BeanUtils 时出现了一个错误

Exception in thread "main" java.lang.reflect.InvocationTargetException: Cannot set id
    at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1017)
    at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:456)
    at com.mizhanke.tpjob.Demo5.main(Demo5.java:68)
Caused by: java.lang.NoSuchMethodException: Property 'id' has no setter method in class 'class com.mizhanke.tpjob.Emp'
    at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2128)
    at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1948)
    at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2054)
    at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1015)
    ... 2 more

Demo5.java

public class Demo5 {
	public static void main(String[] args) throws Exception {
		
		String id = "100";
		String name = "rose";
		String salary = "1000";
		
		
		/*
		 * 如果不是基本数据类型(如:日期格式的字符串,Ojcect类型)  , 则不能自动转换,因为日期格式的字符串是有各种各样的格式
		 * 
		 * 
		 * 解决方法:
		 * 1.直接 new Date(2017, 2, 20)  对象
		 * 2.注册一个类型转换器
		 * 
		 * 一般情况都是使用 方法1
		 * 
		 */
		
		//Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse("2017-02-20");
		
		
		String birthday = "2015-12-06";
		ConvertUtils.register(new Converter() {
			
			@Override
			public Object convert(Class type, Object value) {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				Date date = null;
				try {
					date = format.parse((String) value);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				return date;
			}
		}, Date.class);
		
		
		Emp emp = new Emp();
		BeanUtils.setProperty(emp, "id", id);
		BeanUtils.setProperty(emp, "name", name);
		BeanUtils.setProperty(emp, "salary", salary);
		BeanUtils.setProperty(emp, "birthday", birthday); 
		
		
		System.out.println(emp);
		
	}
}

 

Emp.java

class Emp {
	private int id;
	private String name;
	private double salary;
	private Date birthday;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Emp() {

	}

	public Emp(int id, String name, double salary, Date birthday) {
		this.id = id;
		this.name = name;
		this.salary = salary;
		this.birthday = birthday;
	}

	@Override
	public String toString() {
		return String.format("{id=%d,name=%s,salary=%s,birthday=%s}", id, name,salary,birthday);
	}

}

 

通过和以前的代码对比发现:

以前能运行的代码比  Emp.java  多了一个public 修改符,然后以Emp.java 加上了 public 修饰符就可以运行了。

原因:可能是由于 BeanUtils 和 Emp  不在同一个包下面,所以访问不到。

 

posted @ 2017-02-22 17:04  cppwen  阅读(1057)  评论(0编辑  收藏  举报