title: Android SQLite Quiz App
body: <p>I am trying to create an app that lets you take a quiz, for example, a world history quiz. On the main activity, the user presses the enter button to choose the category of a quiz, let's say we choose world history, then it's supposed to go to the world history activity but it crashes. Here is my code so far, don't know what is wrong with it:</p>
  
  <p>DatabaseHandler.java</p>
  
  <pre><code>public class DatabaseHandler extends SQLiteOpenHelper {
  
      private static final int DATABASE_VERSION = 1;
      private static final String DATABASE_NAME = "quiz";
      private static final String TABLE_NAME = "worldhistory";
      private static final String KEY_ID = "id";
      private static final String QUESTIONS = "question";
      private static final String ANSWER = "answer";
      private static final String OPTIONA = "optiona";
      private static final String OPTIONB = "optionb";
      private static final String OPTIONC = "optionc";
      private static final String OPTIOND = "optiond";
  
      private SQLiteDatabase dba;
  
      public DatabaseHandler(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( "
                  + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + QUESTIONS + " TEXT, "
                  + ANSWER + " TEXT, " + OPTIONA + " TEXT, " + OPTIONB + " TEXT, " + OPTIONC
                  + " TEXT, " + OPTIOND + " TEXT)";
  
          db.execSQL(CREATE_TABLE);
  
          addQuestions();
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          db.execSQL("DROP TABLE IF EXITS " + TABLE_NAME);
          onCreate(db);
      }
  
      // creating the questions, options, and answers
      private void addQuestions() {
          Question q1 = new Question("World War I began in which year?", "1923",
                  "1938", "1917", "1914", "D");
          this.addQuestion(q1);
  
          Question q2 = new Question("Adolf Hitler was born in which country?",
                  "France", "Germany", "Austria", "Hungary", "C");
          this.addQuestion(q2);
  
          //add more questions
      }
  
      // addding new questions to db
      public void addQuestion(Question question) {
          ContentValues values = new ContentValues();
          values.put(QUESTIONS, question.getQuestion());
          values.put(ANSWER, question.getAnswer());
          values.put(OPTIONA, question.getOptionA());
          values.put(OPTIONB, question.getOptionB());
          values.put(OPTIONC, question.getOptionC());
          values.put(OPTIOND, question.getOptionD());
  
          dba.insert(TABLE_NAME, null, values);
      }
  
      public List&lt;Question&gt; getAllQuestions() {
          List&lt;Question&gt; questionList = new ArrayList&lt;Question&gt;();
  
          String query = "SELECT * FROM " + TABLE_NAME;
          dba = this.getReadableDatabase();
          Cursor cursor = dba.rawQuery(query, null);
  
          if (cursor.moveToFirst()) {
              do {
                  Question quest = new Question();
                  quest.setID(cursor.getInt(0));
                  quest.setQuestion(cursor.getString(1));
                  quest.setAnswer(cursor.getString(2));
                  quest.setOptionA(cursor.getString(3));
                  quest.setOptionB(cursor.getString(4));
                  quest.setOptionC(cursor.getString(5));
                  quest.setOptionD(cursor.getString(6));
  
                  questionList.add(quest);
  
              } while (cursor.moveToNext());
          }
  
          cursor.close();
          dba.close();
          return questionList;
      }
  
      public int rowCount() {
          int row = 0;
          String query = "SELECT * FROM " + TABLE_NAME;
          SQLiteDatabase db = this.getWritableDatabase();
          Cursor cursor = db.rawQuery(query, null);
          row = cursor.getCount();
  
          cursor.close();
          return row;
      }
  }
  </code></pre>
  
  <p>Question.java</p>
  
  <pre><code>public class Question {
      private int ID;
      private String Question, optionA, optionB, optionC, optionD, Answer;
  
      public Question() {
          ID = 0;
          Question = "";
          optionA = "";
          optionB = "";
          optionC = "";
          optionD = "";
          Answer = "";
      }
  
      public Question(String Question, String optionA, String optionB, String optionC,
                      String optionD, String Answer) {
          this.Question = Question;
          this.optionA = optionA;
          this.optionB = optionB;
          this.optionC = optionC;
          this.optionD = optionD;
          this.Answer = Answer;
      }
  
      public int getID() {
          return ID;
      }
  
      public String getQuestion() {
          return Question;
      }
  
      public String getOptionA() {
          return optionA;
      }
  
      public String getOptionB() {
          return optionB;
      }
  
      public String getOptionC() {
          return optionC;
      }
  
      public String getOptionD() {
          return optionD;
      }
  
      public String getAnswer() {
          return Answer;
      }
  
      public void setID(int ID) {
          this.ID = ID;
      }
  
      public void setQuestion(String Question) {
          this.Question = Question;
      }
  
      public void setOptionA(String optionA) {
          this.optionA = optionA;
      }
  
      public void setOptionB(String optionB) {
          this.optionB = optionB;
      }
  
      public void setOptionC(String optionC) {
          this.optionC = optionC;
      }
  
      public void setOptionD(String optionD) {
          this.optionD = optionD;
      }
  
      public void setAnswer(String answer) {
          Answer = answer;
      }
  }
  </code></pre>
  
  <p>WorldHistory.java</p>
  
  <pre><code>public class WorldHistory extends AppCompatActivity {
      List&lt;Question&gt; questionList;
      int score = 0;
      int questionID = 0;
  
      Question currentQuestion;
      TextView txtQuestion;
      RadioButton rbtnA, rbtnB, rbtnC, rbtnD;
      Button btnNext;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_world_history);
  
          DatabaseHandler dba = new DatabaseHandler(this);
          questionList = dba.getAllQuestions();
          currentQuestion = questionList.get(questionID);
  
          txtQuestion = (TextView) findViewById(R.id.txtQuestion);
          rbtnA = (RadioButton) findViewById(R.id.rtbOptionA);
          rbtnB = (RadioButton) findViewById(R.id.rtbOptionB);
          rbtnC = (RadioButton) findViewById(R.id.rtbOptionC);
          rbtnD = (RadioButton) findViewById(R.id.rtbOptionD);
          btnNext = (Button) findViewById(R.id.btnNext);
  
          setQuestionView();
      }
  
      private void setQuestionView() {
          txtQuestion.setText(currentQuestion.getQuestion());
          rbtnA.setText(currentQuestion.getOptionA());
          rbtnB.setText(currentQuestion.getOptionB());
          rbtnC.setText(currentQuestion.getOptionC());
          rbtnD.setText(currentQuestion.getOptionD());
          questionID++;
      }
  }     
  </code></pre>
  
  <p>Here's the error message:</p>
  
  <pre><code>07-10 20:04:11.346 2514-2514/com.example.nasimahmed.quizgame E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.nasimahmed.quizgame, PID: 2514
  
  
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nasimahmed.quizgame/com.example.nasimahmed.quizgame.WorldHistory}: java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference
  
  
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference
                                                                                     at com.example.nasimahmed.quizgame.DatabaseHandler.addQuestion(DatabaseHandler.java:81)
                                                                                     at com.example.nasimahmed.quizgame.DatabaseHandler.addQuestions(DatabaseHandler.java:61)
                                                                                     at com.example.nasimahmed.quizgame.DatabaseHandler.onCreate(DatabaseHandler.java:46)
                                                                                     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                                     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
                                                                                     at com.example.nasimahmed.quizgame.DatabaseHandler.getAllQuestions(DatabaseHandler.java:89)
                                                                                     at com.example.nasimahmed.quizgame.WorldHistory.onCreate(WorldHistory.java:29)
                                                                                     at android.app.Activity.performCreate(Activity.java:6237)
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:148) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
  </code></pre>