Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:32:57+00:00 2026-06-15T15:32:57+00:00

I am having a NullPointer error on 2 of my lines in my onTick()

  • 0

I am having a NullPointer error on 2 of my lines in my onTick() method. I am new to implementing a subclass and on how to get this timer to work. I just googled CountDownTimers and tried to implement an example I found so it may be a bit messy.

Before I implemented the timer, the app worked great. What I want is for a timer to start as soon as this activity is started, then to restart the timer and display a new question after a click or the timer runs out.

Thank you in advance!

public class QuestionView extends Activity  {

    int correctAnswers = 0;
    int wrongAnswers = 0;

    int answer = 0;

    int i = 0;

    long startTime = 50000;
    long interval = 1000;
    long timeElapsed;

    boolean timerHasStarted = false;

    Button answer1;
    Button answer2;
    Button answer3;
    Button answer4;
    TextView question;
    TextView timer;
    TextView timeElapsedView;

    ArrayList<Question> queries;
    Timer cdTimer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.questionviewmain);

        answer1 = (Button)findViewById(R.id.answer1);
        answer2 = (Button)findViewById(R.id.answer2);
        answer3 = (Button)findViewById(R.id.answer3);
        answer4 = (Button)findViewById(R.id.answer4);

        question = (TextView)findViewById(R.id.question);

        queries = getIntent().getParcelableArrayListExtra("queries");

        cdTimer = new Timer(startTime, interval);

        loadQuestion();
    }

    public void loadQuestion() {

        if(i == 9) {

            endQuiz();

        } else {

            if(!timerHasStarted) {
                cdTimer.start();
                timerHasStarted = true;
            } else {
                cdTimer.cancel();
                timerHasStarted = false;
            }

            answer = queries.get(i).getCorrectAnswer();

            question.setText(queries.get(i).getQuery());

            answer1.setText(queries.get(i).getA1());
            answer2.setText(queries.get(i).getA2());
            answer3.setText(queries.get(i).getA3());
            answer4.setText(queries.get(i).getA4());

            answer1.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(0);
                    if(answer == 0) {
                        correctAnswers++;
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        nextQuestion();
                    }
                }
            });

            answer2.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(1);
                    if(answer == 1) {
                        correctAnswers++;
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        nextQuestion();
                    }
                }
            });

            answer3.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(2);
                    if(answer == 2) {
                        correctAnswers++;
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        nextQuestion();
                    }
                }
            });

            answer4.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    queries.get(i).setSelectedAnswer(3);
                    if(answer == 3) {
                        correctAnswers++;
                        nextQuestion();
                    } else {
                        wrongAnswers++;
                        nextQuestion();
                    }
                }
            });
        } 
    }

    public int getCorrectAnswers() { return correctAnswers; }
    public int getWrongAnswers() { return wrongAnswers; }

    public ArrayList<Question> getQueries() {
        return queries;
    }

    public void nextQuestion() {
        i++;
        loadQuestion();
    }

    public class Timer extends CountDownTimer {

        public Timer(long starttime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            timer.setText("Time's up!");
            timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
        }

        @Override
        public void onTick(long millisUntilFinished) {
            timer.setText("Time remain: " + millisUntilFinished);
            timeElapsed = startTime - millisUntilFinished;
            timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
        }
    }

    public void endQuiz() {
        Intent intent = new Intent(QuestionView.this, Results.class);
        intent.putExtra("correctAnswers", correctAnswers);
        intent.putExtra("wrongAnswers", wrongAnswers);
        intent.putParcelableArrayListExtra("queries", queries);
        startActivity(intent);
    }


}

LogCat

12-05 07:32:06.142: E/AndroidRuntime(6264): FATAL EXCEPTION: main
12-05 07:32:06.142: E/AndroidRuntime(6264): java.lang.NullPointerException
12-05 07:32:06.142: E/AndroidRuntime(6264):     at com.example.test.QuestionView$Timer.onTick(QuestionView.java:164)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at android.os.Looper.loop(Looper.java:137)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at java.lang.reflect.Method.invokeNative(Native Method)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at java.lang.reflect.Method.invoke(Method.java:511)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-05 07:32:06.142: E/AndroidRuntime(6264):     at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:32:59+00:00Added an answer on June 15, 2026 at 3:32 pm

    This may not be the only problem, but you haven’t initialized timer or timeElapsedView.

    You need something like this in onCreate():

    timer = (TextView) findViewById(R.id.timer);
    timeElapsedView = (TextView) findViewById(R.id.timeelapsedView);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having just added a new button in my web application, I get an error
So I'm almost done with this assignment but now I'm having difficulties again. When
Having trouble with an sql statement to retrieve a record between two timestamps. This
having problem with null pointer exception and i read few article bout that error
Having a heckuva time with this one, though I feel I'm missing something obvious.
im having a null pointer error. i tried making the button print by System.out.print
Hello Everyone i am new to android and i am currently stuck on this.
this peice of code initialises X as a new formatter variable. private Formatter X;
I have a method that looks like this: try { doStuff(); } catch (Exception
It was working before but for an unknown reason, I keep having nullpointer exception

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.