I'm developing a "question and answer" Android app. When a user chooses a wrong answer, the game ends and this question and its answer are imported to a database. But I have a problem, I can import the question and its answer to the database but the second action (game is finished and the result is displayed) doesn't work. How can I fix this?
This is my choose answer method:
private void onOptionSelected(String option){
if(!isGamePaused && !isGameEnded) {
ATriviaQuestion tTQuestion = myListOfTriviaQuestions.get(currentQuestionNumber);
if(option.equals(tTQuestion.GetOptions().get(tTQuestion.GetAnswer() - 1))) {
correct += 1;
remainingTime = mySecondsPassed;
totalPoints += remainingTime * pointsPerRemainingSecond;
totalPoints += pointsPerCorrectAnswer;
}
else{ questionsAsked = totalQuestions;
incorrect += 1;
totalPoints -= pointsPerWrongAnswer;
DisplayResults();
EndGame();
database db= new database(this);
db.insertData(new ListOfData(myListOfTriviaQuestions.get(currentQuestionNumber).GetQuestion(),tTQuestion.GetOptions().get(tTQuestion.GetAnswer() - 1)));
loadListViewData(); ;
}
mySecondsPassed = config.GetTimeToAnswer();
UpdateTimerColors(mySecondsPassed);
if(questionsAsked >= totalQuestions){
isGameEnded=true;
EndGame();
DisplayResults();
} else {
currentQuestionNumber=GenerateQuestionNumber();
PopulateQuestion();
}
}else{
PausedOrEndedGameResponse();
}
}
This is the LogCat:
11-11 01:58:29.763: E/AndroidRuntime(276): FATAL EXCEPTION: main
11-11 01:58:29.763: E/AndroidRuntime(276): java.lang.NullPointerException
11-11 01:58:29.763: E/AndroidRuntime(276): at pk.aeh.ideos.taa.PlayGame.lietke_congviec(PlayGame.java:633)
11-11 01:58:29.763: E/AndroidRuntime(276): at pk.aeh.ideos.taa.PlayGame.onOptionSelected(PlayGame.java:229)
11-11 01:58:29.763: E/AndroidRuntime(276): at pk.aeh.ideos.taa.PlayGame.onClick(PlayGame.java:149)
11-11 01:58:29.763: E/AndroidRuntime(276): at android.view.View.performClick(View.java:2408)
11-11 01:58:29.763: E/AndroidRuntime(276): at android.view.View$PerformClick.run(View.java:8816)
11-11 01:58:29.763: E/AndroidRuntime(276): at android.os.Handler.handleCallback(Handler.java:587)
11-11 01:58:29.763: E/AndroidRuntime(276): at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 01:58:29.763: E/AndroidRuntime(276): at android.os.Looper.loop(Looper.java:123)
11-11 01:58:29.763: E/AndroidRuntime(276): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-11 01:58:29.763: E/AndroidRuntime(276): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 01:58:29.763: E/AndroidRuntime(276): at java.lang.reflect.Method.invoke(Method.java:521)
11-11 01:58:29.763: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-11 01:58:29.763: E/AndroidRuntime(276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-11 01:58:29.763: E/AndroidRuntime(276): at dalvik.system.NativeStart.main(Native Method)