Android Studio:NullPointerException。从数据库获取int

Android Studio:NullPointerException。从数据库获取int,android,nullpointerexception,Android,Nullpointerexception,我使用函数中的以下代码从DB中获取Int: Cursor res = mDBHelper.contr(); while (res.moveToNext()) { percA = res.getInt(1);...... 使用此函数设置数字percA的动画后: private void startCountAnimation() { ValueAnimator animator = ValueAnimator.ofInt(0, percA);

我使用函数中的以下代码从DB中获取Int:

 Cursor res = mDBHelper.contr();
    while (res.moveToNext()) {
        percA = res.getInt(1);......
使用此函数设置数字percA的动画后:

private void startCountAnimation() {
          ValueAnimator animator = ValueAnimator.ofInt(0, percA);
    animator.setDuration(1500);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        public void onAnimationUpdate(ValueAnimator animation) {
            percA.setText(animation.getAnimatedValue().toString());
        }
    });
    animator.start();

}
但是这个代码不起作用。错误是: java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“int java.lang.Integer.intValue()”

我试图改变变量percA的属性和函数的属性,但我找不到问题所在。为什么我的变量指向null


谢谢您的帮助。

您希望结果中有多少列?
如果您只等待一列,那么可能应该使用getInt(0)而不是getInt(1)。如果您试图从一个不存在的列中检索数据,那么您就得到了一个NPE。只需询问…

数据库中是否存在空值的可能副本。。。在对该列使用getInt或make Constraint之前,请使用isNull方法检查它。@Zoe不,它不是…@Selvin是的。不,它不是。。。npe不是在他的代码中发生的,也不可能用这个问题的信息来调试它。