-2

i take a Int from DB with this code in a function:

 Cursor res = mDBHelper.contr();
    while (res.moveToNext()) {
        percA = res.getInt(1);......

After i use this function to animate the number 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();

}

But this code doesn't work. The error is: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference.

I try to change the property of variable percA and the property of function but i can't find the problem. Why my variable points to null?

Thanks for your help.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Carl 1
  • 1
  • 1
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Mar 14 '18 at 21:04
  • You have NULL value in the database... Use isNull method to check it before using getInt or make constrain on this column. – Selvin Mar 14 '18 at 21:12
  • @Zoe no, it is not... – Selvin Mar 14 '18 at 21:13
  • @Selvin yes it is. – Zoe Mar 14 '18 at 21:13
  • No, it isn't... It is not in his code where npe occurs, also it is not possible to debug it with information from this question. – Selvin Mar 14 '18 at 21:15
  • @Selvin the object intValue is used on is null. There's no null check before doing that. It is a dupe. If not, there's missing code which makes it off-topic – Zoe Mar 14 '18 at 21:24
  • No, it isn't... **It is called internally by Cursor.getInt implementation.** And he cannot check if something is null there. Eventually it may be off-topic as there is no full stacktrace, but please, read the question before closing it as npe dupe... As you did FX here https://stackoverflow.com/questions/48385221/robolectric-not-handling-getapplicationcontext-correctly-npe fortunately it was reopen. Ive also hate npe in own code questions but some NPE are not in own code and cannot be fixed with given answer. – Selvin Mar 14 '18 at 21:25

1 Answers1

0

how much columns did you expect in the result of your result ? If your waiting for only one column maybe you should getInt(0) and not getInt(1). If your try to retrieve data from a column that doesn't exist you've got a NPE. Just asking ...

olivejp
  • 900
  • 1
  • 9
  • 14