0

The Logcat details of the Error:

06-24 10:23:48.836: W/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-24 10:23:48.865: E/AndroidRuntime(276): FATAL EXCEPTION: main
06-24 10:23:48.865: E/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.triviality/com.example.triviality.InfoActivity}: java.lang.NullPointerException
06-24 10:23:48.865: E/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-24 10:23:48.865: E/AndroidRuntime(276):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-24 10:23:48.865: E/AndroidRuntime(276):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-24 10:23:48.865: E/AndroidRuntime(276):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-24 10:23:48.865: E/AndroidRuntime(276):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 10:23:48.865: E/AndroidRuntime(276):  at android.os.Looper.loop(Looper.java:123)
06-24 10:23:48.865: E/AndroidRuntime(276):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-24 10:23:48.865: E/AndroidRuntime(276):  at java.lang.reflect.Method.invokeNative(Native Method)
06-24 10:23:48.865: E/AndroidRuntime(276):  at java.lang.reflect.Method.invoke(Method.java:521)
06-24 10:23:48.865: E/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-24 10:23:48.865: E/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-24 10:23:48.865: E/AndroidRuntime(276):  at dalvik.system.NativeStart.main(Native Method)
06-24 10:23:48.865: E/AndroidRuntime(276): Caused by: java.lang.NullPointerException
06-24 10:23:48.865: E/AndroidRuntime(276):  at com.example.triviality.InfoActivity.onCreate(InfoActivity.java:32)
06-24 10:23:48.865: E/AndroidRuntime(276):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-24 10:23:48.865: E/AndroidRuntime(276):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-24 10:23:48.865: E/AndroidRuntime(276):  ... 11 more
06-24 10:23:59.235: I/Process(276): Sending signal. PID: 276 SIG: 9

There are 2 classes, QuizActivity and InfoActivity. If the Category value is 2 then i need to display the QuizActivity and other than 2 then i need to display the InfoActivity. So I am calling QuizActivity, InfoActivity based on Category in both programs.

QuizActivity.java

package com.example.triviality;
import java.util.List;

import com.example.triviality.R.raw;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.media.MediaPlayer;
public class QuizActivity extends Activity {
    List<Question> quesList;
    int score=0;
    public static int qid=0;
    Question currentQ;
    TextView txtQuestion,txtQuestionnum, txtPoints,txtExplanation;
    RadioButton rda, rdb, rdc, rdd;
    public static int Qno=0;
    Button butNext;
    public MediaPlayer mpr,mpw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        DbHelper db=new DbHelper(this);
        quesList=db.getAllQuestions();
        currentQ=quesList.get(qid);
        txtQuestionnum=(TextView)findViewById(R.id.textView2);
        txtPoints=(TextView)findViewById(R.id.textView3);
        txtQuestion=(TextView)findViewById(R.id.textView1);
        rda=(RadioButton)findViewById(R.id.radio0);
        rdb=(RadioButton)findViewById(R.id.radio1);
        rdc=(RadioButton)findViewById(R.id.radio2);
        rdd=(RadioButton)findViewById(R.id.radio3);
        butNext=(Button)findViewById(R.id.button1);
        mpr = MediaPlayer.create(QuizActivity.this, raw.right);
        mpw = MediaPlayer.create(QuizActivity.this, raw.wrong);
        setQuestionView();
        butNext.setOnClickListener(new View.OnClickListener() {     
            @Override
            public void onClick(View v) {
            if (currentQ.getCATEGORY().equals("2"))
            {   
                RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
                RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
                Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
                if(currentQ.getANSWER().equals(answer.getText()))
                {

                    callanswer("Right");
                    score= score + 5;
                    Log.d("score", "Your score"+score);
                }
                else
                {
                    score= score - 2;
                    callanswer("Wrong");
                }
            }
            else
            {
                Intent intent = new Intent(QuizActivity.this, InfoActivity.class);
                startActivity(intent);
            }
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_quiz, menu);
        return true;
    }
    public void callanswer(String Status)
    {
       txtPoints.setText("Points: " + score);   
       String messageVal = "Correct Answer is : ";
       if (Status.equals("Right"))
       {
          mpr.start(); 
          messageVal = "Answer is : ";
       }
       else
       { 
          mpw.start();  
       }
       messageVal = messageVal + currentQ.getANSWER()+"\n"; 
       messageVal = messageVal + "More Info: " + currentQ.getDETAIL();
       AlertDialog.Builder alertDialog  = new AlertDialog.Builder(this);
       alertDialog.setTitle(" ");
       if (Status.equals("Right"))
       {       
        alertDialog.setIcon(R.drawable.right);
       }
       else
       {
           alertDialog.setIcon(R.drawable.wrong);   
       }

        alertDialog.setMessage(messageVal); 
        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                if(Qno<10){     
                    currentQ=quesList.get(qid);
                    setQuestionView();
                }else{
                    Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
                    Bundle b = new Bundle();
                    b.putInt("score", score); //Your score
                    intent.putExtras(b); //Put your score to your next Intent
                    startActivity(intent);
                    finish();
                }                   
            }
        });
        alertDialog.show();

    }
    private void setQuestionView()
    {
        if (currentQ.getCATEGORY().equals("2"))
        {   
        setContentView(R.layout.activity_quiz); 
        txtQuestionnum.setText("Question No: "+ ++Qno);
        txtPoints.setText("Points: " + score);
        txtQuestion.setText(currentQ.getQUESTION());
        rda.setText(currentQ.getOPTA());
        rdb.setText(currentQ.getOPTB());
        rdc.setText(currentQ.getOPTC());
        rdd.setText(currentQ.getOPTD());
        qid++;
        }
        else
        {
            Intent intent = new Intent(this, InfoActivity.class);
            startActivity(intent);  
        }
    }
}

InfoActivity.java

package com.example.triviality;
import java.util.List;

import com.example.triviality.R.raw;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.media.MediaPlayer;
public class InfoActivity extends Activity {
    List<Question> quesList;
    int score=0;
    Question currentQ;
    TextView txtQuestion,txtQuestionnum, txtPoints,txtExplanation;
    RadioButton rda, rdb, rdc, rdd;
    public static int Qno=0;
    Button butNext;
    public MediaPlayer mpr,mpw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        currentQ=quesList.get(QuizActivity.qid);
        txtQuestion=(TextView)findViewById(R.id.textView1);
        txtExplanation=(TextView)findViewById(R.id.textView6);
        setQuestionView();
        butNext.setOnClickListener(new View.OnClickListener() {     
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(InfoActivity.this, QuizActivity.class);
                startActivity(intent);
            }
        });
    }

    private void setQuestionView()
    {
        Log.d("category",currentQ.getCATEGORY());
        if (currentQ.getCATEGORY().equals("2"))
        {   
            Intent intent = new Intent(InfoActivity.this, QuizActivity.class);
            startActivity(intent);
        }
        else
        {
            txtQuestion.setText(currentQ.getQUESTION());
            txtExplanation.setText(currentQ.getCATEGORY());
            QuizActivity.qid++;
        }

    }
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
user3326228
  • 51
  • 1
  • 9

1 Answers1

1

You have this declared

 List<Question> quesList;

It is not initialized any where and when you have currentQ=quesList.get(QuizActivity.qid) leads to NullPointerException. Even if you initialize the list is still not populated with items.

Note : Do not use setContentView with differennt layout set to the same activity. Its not a good idea. Each activity has its own ui and lifecycle of its own.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Raghu just out of curiosity I'm asking, when we declare a variable but do not initialize it then why don't we get a compile time error or in other case we shouldn't be able to operate on the collection only. – CodeWarrior Jun 24 '14 at 05:44
  • @AndroidWarrior read http://stackoverflow.com/questions/12882448/compile-time-vs-run-time-errors – Raghunandan Jun 24 '14 at 05:46
  • That doesn't solve my query. My question was that why does the IDE not give intellicense in these cases so we may avoid such questions coming up here on the forums. – CodeWarrior Jun 24 '14 at 05:59
  • no it does not. Few posts there mention the same there – Raghunandan Jun 24 '14 at 06:00
  • Raghu, Thanks a lot. By the way please let me know how you figured out the exact place, becz i am a mainframe developer but learning android and one of my app is: https://play.google.com/store/apps/details?id=com.coreservlets.mathsgeniusfree – user3326228 Jun 24 '14 at 06:00
  • @user3326228 by looking at **Caused by: java.lang.NullPointerException 06-24 10:23:48.865: E/AndroidRuntime(276): at com.example.triviality.InfoActivity.onCreate(InfoActivity.java:32)**. See line 32 – Raghunandan Jun 24 '14 at 06:02