NullPointerException


#1

public class QuizActivity extends AppCompatActivity {

private Button mTrueButton;
private Button mFalseButon;
private Button mNextBuuttonn;
private Button mPrevButton;
private TextView mQuestionTextView;

private Question [] mQuestionBank = new Question[]{
        new Question(R.string.question_australia, true),
        new Question(R.string.question_oceans, true),
        new Question(R.string.question_mideast, false),
        new Question(R.string.question_africa, false),
        new Question(R.string.question_americas, true),
        new Question(R.string.question_asia, true),
};

private int mCurrentIndex = 0;


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

    mQuestionTextView = (TextView) findViewById(R.id.question_text_view);

    mTrueButton = (Button) findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkAnswer(true);

        }
    });

    mFalseButon = (Button) findViewById(R.id.false_button);
    mFalseButon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           checkAnswer(false);

        }
    });

    mNextBuuttonn = (Button) findViewById(R.id.next_button);
    mNextBuuttonn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            updateQuestion();
        }
    });

    mPrevButton = (Button) findViewById(R.id.prev_button);
    mPrevButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCurrentIndex = Math.abs((mCurrentIndex - 1) % mQuestionBank.length);
            updateQuestion();
        }
    });

    updateQuestion();

}

private void updateQuestion(){
    int question = mQuestionBank[mCurrentIndex].getTextResId();
    mQuestionTextView.setText(question);

}

private void checkAnswer (boolean userPressedTrue) {
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswertrue();
    int messageResId = 0;
    if(userPressedTrue == answerIsTrue) {
        messageResId = R.string.correct_toast;
    }
    else {
        messageResId = R.string.incorrect_toast;
    }

    Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
}

}

at com.bignerdranch.android.geoquiz.QuizActivity.updateQuestion(QuizActivity.java:88)
at com.bignerdranch.android.geoquiz.QuizActivity.onCreate(QuizActivity.java:82)

I tried code from archive, but it didn`t help


#2

You have not explained your problem very well!!

Even then I tried to run your code on my computer and found a bug!!

Why have you used the getTextResId() method in the updateQuestion() function??
Android studio doesn’t even recognise the getTextResId() method.

int question = mQuestionBank[mCurrentIndex].getTextResId();//WHY USE THIS??

In my code I replaced the getTextResId() method with the getQuestion() method which is declared in the TrueFalse.java file and it works!!

int question = mQuestionBank[mCurrentIndex].getQuestion();

You can do the same stuff with your code and I hope that it will work.

In any case please let me know :slightly_smiling_face:


#3

package com.bignerdranch.android.geoquiz;

public class Question {

private int mTextResId;
private boolean mAnswerTrue;

public Question(int textResId, boolean answerTrue) {
    mTextResId = textResId;
    mAnswerTrue = answerTrue;
}

public int getTextResId() {
    return mTextResId;
}

public void setTextResId(int textResId) {
    mTextResId = textResId;
}

public boolean isAnswerTrue() {
    return mAnswerTrue;
}

public void setAnswerTrue(boolean answerTrue) {
    mAnswerTrue = answerTrue;
}

}

this is code of object ‘Question’ from book. getTextResId() it’s a getter Question’s class, with give id.

I took this code from book but had an exception, after I download archive and took code from it - but had exception again.


#4

Where did you find metod getQuestion() in charper 2 ? I try to find mistake in book’s code and solve it.


#5

I don’t know which book you’re talking about!!
There is no Question class in chapter 2, in the beginning of chapter 2 the TrueFalse.java file is created and in that class the getQuestion() method is created.

I do not know from where you got the Question class that you have stated in your previous message.


#6

In the 3rd edition it uses a question class. I am having the same problem with the question class and getRes.Id