Search code examples
androidandroid-studiocountdowntimer

Countdowntimer cancel crashes the app when click on button


I am trying to create a countDownTimer but, whenever I click on the button the app crashes. I want that if I click on player1 button player2 countdown should pause and vice versa for player2.

The Error I am getting:

Attempt to invoke virtual method 'void android.os.CountDownTimer.cancel()' on a null object reference

Here is the Code

public class Timer extends AppCompatActivity {

    Button player1, player2;
    long incrementTime = 3000;
    int time;
    long timeinLong;
    ImageButton pause;
    CountDownTimer player1count, player2count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer);

        player1 = findViewById(R.id.player1);
        player2 = findViewById(R.id.player2);

        Intent intent = getIntent();
        time = intent.getIntExtra("time", 0);
        timeinLong = time * 60000;

        int minutes = (int) (timeinLong / 1000) / 60;
        int seconds = (int) (timeinLong / 1000) % 60;

        String timeFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);

        player1.setText(timeFormatted);
        player1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startCountDown(player1);
                player2count.cancel();

            }
        });
        player2.setText(timeFormatted);
        player2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startCountDown2(player2);
                player1count.cancel();
            }
        });

    }

    private void startCountDown(final Button button) {
        player1count = new CountDownTimer(timeinLong, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeinLong = millisUntilFinished;
                updateCountDownText(button);
            }

            @Override
            public void onFinish() {
                timeinLong = 0;
                updateCountDownText(button);
            }
        }.start();
    }

    private void startCountDown2(final Button button) {
        player2count = new CountDownTimer(timeinLong, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeinLong = millisUntilFinished;
                updateCountDownText(button);
            }

            @Override
            public void onFinish() {
                timeinLong = 0;
                updateCountDownText(button);
            }
        }.start();
    }

    private void updateCountDownText(Button button) {
        int minutes = (int) (timeinLong / 1000) / 60;
        int seconds = (int) (timeinLong / 1000) % 60;

        String timeFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);

        button.setText(timeFormatted);

        if (timeinLong < 30000) {
            button.setTextColor(Color.RED);
        } else {
            button.setTextColor(Color.BLACK);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player1count != null) {
            player1count.cancel();
        }
    }
} 

Solution

  • player2count only gets initialized inside startCountDown2, which is only called inside player2.setOnClickListener. Hence, if you click on the player1 button before ever clicking on player2 button, you'll get a NullPointerException, since calling player2count.cancel() on a not-yet-initialized player2count is not allowed. To prevent this from happening, it would be appropriate to just check for null in this case:

    if (player2count != null) {
      player2count.cancel();
    }