Reputation: 35
I am new to android programming so I am developing a quiz app for practice.. Every thing is working fine but there is one error and I can't find solution of it. The problem is when the first question appears and if I don't answer it and click next button it gives me error java.lang.nullpointerException. I have used sqlite for storing questions and I have used timer as time finishes all the radio buttons get disabled.
Here is my code:
private TextView tv_timer;
List<Question> question_list;
int score = 0;
int qid = 0;
Question currentQ;
TextView tv_question;
RadioButton rda,rdb,rdc,rdd,rde;
Button btn_next;
MyCount counter;
RadioGroup rg;
RadioButton answer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_timer = (TextView)findViewById(R.id.tv_timer);
counter = new MyCount(50000,1000);
QuizHelper qh = new QuizHelper(this);
question_list=qh.getAllQuestions();
currentQ = question_list.get(qid);
tv_question = (TextView)findViewById(R.id.tv_question);
rda = (RadioButton)findViewById(R.id.radio1);
rdb = (RadioButton)findViewById(R.id.radio2);
rdc = (RadioButton)findViewById(R.id.radio3);
rdd = (RadioButton)findViewById(R.id.radio4);
rde = (RadioButton)findViewById(R.id.radio5);
btn_next = (Button)findViewById(R.id.button1);
setQuestionView();
btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
try{
rda.setChecked(false);
rdb.setChecked(false);
rdc.setChecked(false);
rdd.setChecked(false);
rde.setChecked(false);
rda.setClickable(true);
rdb.setClickable(true);
rdc.setClickable(true);
rdd.setClickable(true);
rde.setClickable(true);
rg = (RadioGroup)findViewById(R.id.RadioGroup1);
answer = (RadioButton)findViewById(rg.getCheckedRadioButtonId());
if(currentQ.getANSWER().equals(answer.getText()))
{
score++;
}
if(qid < 5){
currentQ = question_list.get(qid);
setQuestionView();
}
else{
Intent i = new Intent(MainActivity.this,ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score);
i.putExtras(b);
startActivity(i);
finish();
}
}catch(Exception e){
Log.d("Error","" + e);
}
}
});
}
public void setQuestionView(){
counter.start();
tv_question.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
rdd.setText(currentQ.getOPTD());
rde.setText(currentQ.getOPTE());
qid++;
}
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
rda.setClickable(false);
rdb.setClickable(false);
rdc.setClickable(false);
rdd.setClickable(false);
rde.setClickable(false);
}
Somebody please help :)
Here is logcat:
09-02 01:13:58.958: D/AndroidRuntime(1348): Shutting down VM
09-02 01:13:58.958: W/dalvikvm(1348): threadid=1: thread exiting with uncaught exception (group=0xb1b0aba8)
09-02 01:13:59.018: E/AndroidRuntime(1348): FATAL EXCEPTION: main
09-02 01:13:59.018: E/AndroidRuntime(1348): Process: com.example.challengeyourself, PID: 1348
09-02 01:13:59.018: E/AndroidRuntime(1348): java.lang.NullPointerException
09-02 01:13:59.018: E/AndroidRuntime(1348): at com.example.challengeyourself.MainActivity$1.onClick(MainActivity.java:84)
09-02 01:13:59.018: E/AndroidRuntime(1348): at android.view.View.performClick(View.java:4438)
09-02 01:13:59.018: E/AndroidRuntime(1348): at android.view.View$PerformClick.run(View.java:18422)
09-02 01:13:59.018: E/AndroidRuntime(1348): at android.os.Handler.handleCallback(Handler.java:733)
09-02 01:13:59.018: E/AndroidRuntime(1348): at android.os.Handler.dispatchMessage(Handler.java:95)
09-02 01:13:59.018: E/AndroidRuntime(1348): at android.os.Looper.loop(Looper.java:136)
09-02 01:13:59.018: E/AndroidRuntime(1348): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-02 01:13:59.018: E/AndroidRuntime(1348): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 01:13:59.018: E/AndroidRuntime(1348): at java.lang.reflect.Method.invoke(Method.java:515)
09-02 01:13:59.018: E/AndroidRuntime(1348): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-02 01:13:59.018: E/AndroidRuntime(1348): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-02 01:13:59.018: E/AndroidRuntime(1348): at dalvik.system.NativeStart.main(Native Method)
09-02 01:14:02.588: I/Process(1348): Sending signal. PID: 1348 SIG: 9
09-02 01:14:05.648: D/dalvikvm(1374): GC_FOR_ALLOC freed 58K, 5% free 3036K/3168K, paused 169ms, total 173ms
09-02 01:14:05.698: I/dalvikvm-heap(1374): Grow heap (frag case) to 8.390MB for 5622016-byte allocation
09-02 01:14:05.748: D/dalvikvm(1374): GC_FOR_ALLOC freed 2K, 2% free 8524K/8660K, paused 42ms, total 43ms
09-02 01:14:06.328: D/gralloc_goldfish(1374): Emulator without GPU emulation detected.
Upvotes: 0
Views: 212
Reputation: 28823
You are getting NullPointerException
on
if(currentQ.getANSWER().equals(answer.getText()))
as you have not checked any radio button. Hence answer will be null at that time.
Put a check before you try to access answer's text:
if(answer != null){
if(currentQ.getANSWER().equals(answer.getText())){
//do something
}
}
Hope this helps.
P.S: Always initialize all your views in onCreate
. This way they are being initialized in onClick
every time you click on Next. That's not a good practice.
Upvotes: 1
Reputation: 14398
initialize rg
and answer
in onCreate method i.e. before btn_next.setOnClickListener
because those are part of activity layout.
i.e. Rewrite as
rg = (RadioGroup)findViewById(R.id.RadioGroup1);
answer = (RadioButton)findViewById(rg.getCheckedRadioButtonId());
btn_next.setOnClickListener(new View.OnClickListener() {
and don't forget to remove
rg = (RadioGroup)findViewById(R.id.RadioGroup1);
answer = (RadioButton)findViewById(rg.getCheckedRadioButtonId());
from setOnClickListener
Upvotes: 0
Reputation: 5535
Did you initialize these variable?
Button btn_next;
like this
btn_next= (Button)findViewById(R.id.yourbutton);
try to put this above code in OnCreate method of your activity orjust before you set the listinener of your button
btn_next= (Button)findViewById(R.id.yourbutton);
btn_next.setOnClickListener(new View.OnClickListener()..........
and i think btn_next.setOnClickListiner
is giving the nullpointer exception as you havent initialized it .
Upvotes: 0