1

I got this error

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

Maybe this error comes from adding into database and cause's error. java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

Am I converting bitmap correctly into database?

Maybe that causes the error on array :)

DbHelper.java

package com.example.quizjavatest;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.BitmapFactory;

public class DbHelper extends SQLiteOpenHelper{

    private Context mContext;
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "PediaKids";
    // tasks table name

    private static final String TABLE_QUEST = "planet";

    // tasks Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_IMG = "img";
    private static final String KEY_QUES = "question";
    private static final String KEY_ANSWER = "answer"; //correct option
    private static final String KEY_OPTA= "opta"; //option a
    private static final String KEY_OPTB= "optb"; //option b
    private static final String KEY_OPTC= "optc"; //option c
    private SQLiteDatabase dbase;
    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mContext = context;

    }
        @Override
        public void onCreate(SQLiteDatabase db) {



            dbase=db;
            String Sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+ KEY_IMG + " BLOB," + KEY_QUES
                    + " TEXT, "  + KEY_OPTA +" TEXT, " + KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT," + KEY_ANSWER + " TEXT)";



            db.execSQL(Sql);
            addQuestion();


            //db.close();
        }
        private void addQuestion()
        {
            Question q1=new Question(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher),"an operating system?", "SuSe", "BIOS", "DOS", "BIOS");
            this.addQuestion(q1);
            Question q2=new Question(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher),"Which of the following is NOT  "," SuSe", "BIOS", "DOS", "BIOS");
            this.addQuestion(q2);
            Question q3=new Question(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher),"Which of the following is the fastest","RAM", "FLASH","Register","Register");
            this.addQuestion(q3);
            Question q4=new Question(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher),"Which of the following device", "Router", "Bridge", "Hub","Router");
            this.addQuestion(q4);
            Question q5=new Question(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher),"Which of the following is NOT an" ,"Ruby","Python","BASIC","BASIC");
            this.addQuestion(q5);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
            // Drop older table if existed

            db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);

            // Create tables again
            onCreate(db);
        }

        // Adding new question

        public void addQuestion(Question quest) {
            //SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            //values.put(KEY_ID, quest.getID());
            //values.put(KEY_IMG, Utility.getBytes(quest.getBitmap()));
            values.put(KEY_QUES, quest.getQUESTION()); 
            values.put(KEY_ANSWER, quest.getANSWER());
            values.put(KEY_OPTA, quest.getOPTA());
            values.put(KEY_OPTB, quest.getOPTB());
            values.put(KEY_OPTC, quest.getOPTC());
            // Inserting Row
            dbase.insert(TABLE_QUEST, null, values);


        }


        public List<Question> getAllQuestions(){
            List<Question> quesList = new ArrayList<Question>();
            String selectQuery = "SELECT * FROM " + TABLE_QUEST + " ORDER BY RANDOM() LIMIT 5";
            dbase=this.getReadableDatabase();
            Cursor cursor = dbase.rawQuery(selectQuery, null);
            // looping through all rows and adding to list
            //==============================================
            if (cursor.moveToFirst()) {
                do {
                    Question quest = new Question();

                    quest.setID(cursor.getInt(0));
                    //quest.setImage(Utility.getPhoto(cursor.getBlob(1)));
                    quest.setQUESTION(cursor.getString(1));
                    quest.setANSWER(cursor.getString(2));
                    quest.setOPTA(cursor.getString(3));
                    quest.setOPTB(cursor.getString(4));
                    quest.setOPTC(cursor.getString(5));
                    quesList.add(quest);
                } while (cursor.moveToNext());
            }
            // return quest list
            return quesList;
        }
        //==============================================

        public int rowcount()
        {
            int row=0;
            String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);
            row=cursor.getCount();
            return row;
        }
    }
Jonny C
  • 1,943
  • 3
  • 20
  • 36

1 Answers1

0

Try reinstalling the app. Reinstalling means, you'll have to completley unistall the app and then do a fresh install of the app.

This error occurs because, while developing the app, if you have made any changes that alter the structure of the database after you have installed the app once, then the older version of the app is unable to find the change.

Thus a reinstall of the app helps..

Lal
  • 14,726
  • 4
  • 45
  • 70