-4

This method renders some text onto a Canvas every time the screen refreshes using a SurfaceView and is supposed to return when the database cursor is closed (if cursorClosed). But as you can see from the LogCat, it doesnt. Even though cursorClosed is true as evidenced by the System.out.println(cursorClosed) call it arrives at this call when it should have returned after the if-statement. Can anybody explain to me why this is happening? Thank you for every hint!

This is my code:

public void present(float deltaTime) {
        Graphics graphics = game.getGraphics();
        graphics.drawBackground(Assets.background);
        if (cursorClosed)
            return;
        System.out.println(cursorClosed);
        if (picture != null) {
            graphics.drawPixmap(picture, 40, 280, 50);
        }
        if (!translated) {
            graphics.renderText(14, cursor.getString(1), 160, 300);
        } else {
            System.out.println("cursorClosed: " + cursorClosed);
        graphics.renderText(14, cursor.getString(2), 160, 300);
        graphics.drawPixmap(Assets.nextButton, 250, 310, 420);
    }
}

This is the LogCat:

07-20 19:21:25.950 21343-21378/com.melonman.wordsandphrases I/System.out: false
07-20 19:21:25.950 21343-21343/com.melonman.wordsandphrases I/System.out: 1
07-20 19:21:25.950 21343-21378/com.melonman.wordsandphrases I/System.out: cursorClosed: true
07-20 19:21:25.950 21343-21378/com.melonman.wordsandphrases E/AndroidRuntime: FATAL EXCEPTION: Thread-35292
Process: com.melonman.wordsandphrases, PID: 21343
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.database.Cursor.getString(int)' on a null object reference
    at com.melonman.wordsandphrases.wordsandphrasesapp.LessonScreen1.present(LessonScreen1.java:83)
    at com.melonman.wordsandphrases.framework.impl.AndroidFastRenderView.run(AndroidFastRenderView.java:46)
    at java.lang.Thread.run(Thread.java:818)
Einzig7
  • 543
  • 1
  • 6
  • 22
Lrnz
  • 1
  • 2
  • What is "cursorClsoed"? I can't see it is initialized in your code. Maybe check if isn't null firstly – Kacper Jul 21 '18 at 10:11
  • @K.Kempski - If it were a nullable type, the code wouldn't compile. – T.J. Crowder Jul 21 '18 at 10:16
  • The log output clearly shows that it's `false` just after being tested, and only `true` **later**. So clearly something is changing its value in the interim. (Perhaps the same thing that's logging `1`.) – T.J. Crowder Jul 21 '18 at 10:18
  • @T.J.Crowder I think this question is not a duplicate, at least not for the reason you provided. This is a logical issue that eventually reaches NullPointrException. OP thinks that his code would hit return and does not understand why it doesn't. This is exactly what you mentioned in your comment. –  Jul 21 '18 at 10:31
  • 1
    @mTak - **I** didn't vote to close as duplicate. [This is one of my pet peeves about SO](https://meta.stackexchange.com/questions/269073/dont-say-i-marked-something-as-a-duplicate-when-i-didnt). I voted to close as non-repro/not-useful-in-future as something as the question asks why the `if` didn't test `true`, and the answer is clearly "because it wasn't `true` at the time." – T.J. Crowder Jul 21 '18 at 10:34
  • @T.J.Crowder I saw this: *marked as duplicate by T.J. Crowder, EJP*. So how was it decided to be marked as duplicate? I don't know the procedure, is there a link where I can get informed about it? –  Jul 21 '18 at 10:38
  • @mTak - See my link above and [the supposed duplicate it was closed in favor of](https://meta.stackexchange.com/questions/54917/distinguish-close-votes-by-reason) (which I just added a bounty to). EJP dupehammered it. I voted to close for an entirely different reason. – T.J. Crowder Jul 21 '18 at 10:39

1 Answers1

0

This code:

    if (cursorClosed)
        return;
    System.out.println(cursorClosed);

prints false this is why the return statement does not execute.

This code:

    if (!translated) {
        graphics.renderText(14, cursor.getString(1), 160, 300);
    } else {
        System.out.println("cursorClosed: " + cursorClosed);

prints cursorClosed: true