java 反射 報錯:Attempt to get java.lang.Integer field "..." with illegal data type conversion to int

不知為何就叫呵呵發表於2017-07-27

類: Integer id;

反射時:

Field f = User.class.getDeclaredField("id"); 

f.setAccessible(true);

int s = f.getInt(u);

這樣會報錯:

java.lang.IllegalArgumentException: Attempt to get java.lang.Integer field "..." with illegal data type conversion to int

改為:

int s = (Integer) f.get(u);

 

相關文章