我正在获取NoSuchFieldException
以下代码:
public class MultipleSorting<T> extends Observable {
private SelectItem[] criteria1;
private SelectItem[] order1;
private SelectItem[] criteria2;
private SelectItem[] order2;
private SelectItem[] criteria3;
private SelectItem[] order3;
private T criteriaType;
private T selectedCriteria1;
private SortOrder selectedOrder1;
private T selectedCriteria2;
private SortOrder selectedOrder2;
private T selectedCriteria3;
private SortOrder selectedOrder3;
private Boolean[] enabledRows = new Boolean[]{Boolean.TRUE, Boolean.FALSE, Boolean.FALSE};
private Boolean addButtonVisible1 = Boolean.TRUE;
private Boolean addButtonVisible2 = Boolean.FALSE;
private Boolean addButtonVisible3 = Boolean.FALSE;
public MultipleSorting() {
super();
}
private Class<T> getCriteriaClass() throws NoSuchFieldException {
Field field = this.getClass().getField("criteriaType");
field.setAccessible(true);
return (Class<T>)field.getType();
}
public void addOrRemoveRow(ActionEvent event) {
// other codes
Method setSelectedCriteriaMethod = getClass().getDeclaredMethod("setSelectedCriteria" + (index + 1), new Class[]{getCriteriaClass()});
// other codes
}
// getters and setters
}
调用该方法时出现异常getCriteriaClass()
。criteriaType
没有任何 getter 和 seeter 方法。此字段也未初始化。这就是为什么我不能打电话criteriaType.getClass()
,因为它正在投掷NullPointerException
。
我的目标是确定类的类型,T
我不想在这个类的构造函数中传递 T 的MultipleSorting
类。
我无法理解为什么我会得到NoSuchFieldException
. 任何指针都会对我很有帮助。