public class TestBean {
private String[] array;
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
}
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
public class BeanUtilTest {
public static void main(String[] args) {
TestBean bean = new TestBean();
try {
BeanUtils.setProperty(bean, "array[0]", "zero");
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行代码时,会出现以下异常
java.lang.NullPointerException
at org.apache.commons.beanutils.PropertyUtilsBean.setIndexedProperty(PropertyUtilsBean.java:1414)
at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1016)
at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:313)
at BeanUtilTest.main(BeanUtilTest.java:10)
运行时数组大小会增加。所以我不想要一个固定大小的数组。应在运行时定义数组大小。
答案 0 :(得分:0)
您需要初始化数组或使用
设置数组BeanUtils.setProperty(bean, "array", array);
然后使用以下操作设置值
BeanUtils.setProperty(bean, "array[0]", "zero");
运行期间数组大小不会增加