0 Replies - 3532 Views - Last Post: 03 July 2012 - 04:56 AM

#1 swarnika  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-October 08

IlegalStateException when retrieving data from SQLite.

Posted 03 July 2012 - 04:56 AM

we are developing a small reminder app in android, but it gives IllegalStateException when we access the records.
Here is the code:
Database.java
package com.pack.Project_Reminder;
import com.pack.*;
import java.util.ArrayList;
import java.util.List;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Database {

	private static final String DATABASE_NAME="project9.db";
	private static final String TABLE_BIRTH_ANN="birth_ann";
	private static final String TABLE_PAR_MEET="par_meet";
	private static final int DATABASE_VERSION = 1;
	private final Context context;
	private SQLiteDatabase db;
	private ToDoHelper toDoHelper;
	
	public Database(Context context){
		this.context=context;
		 toDoHelper = new ToDoHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	public static final String key_id = "id";
	public static final String Date = "date";
	public static final String Name = "name";
	public static final String Description = "description";
	public static final String Time = "time";
	public static final String CREATE_BIRTH_ANN =
"create table "+TABLE_BIRTH_ANN+" ("+key_id+" INTEGER PRIMARY KEY AUTOINCREMENT, " +Date+ " date," + Name+ " text, " + Description+ " text);";
	
		public static class ToDoHelper extends SQLiteOpenHelper
	{
		public ToDoHelper(Context c, String name, CursorFactory cf, int version){
			super(c,name,cf,version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(CREATE_BIRTH_ANN);
					}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
		}
	}
	
	
	public void close()
	{
		db.close();
	}
	public void open() throws SQLiteException
	{
		try
		{
			db=toDoHelper.getWritableDatabase();
		}
		catch(SQLiteException ex)
		{
			db=toDoHelper.getReadableDatabase();
		}
	}
	public long insert_birth_ann(String d,String nm,String des)
	{
		ContentValues cv=new ContentValues();
		cv.put("date",d.toString());
		cv.put("name",nm.toString());
		cv.put("description", des.toString());
		return db.insert(TABLE_BIRTH_ANN, null, cv);
	}
		public Cursor get_bdy(int pos)
	{
		
		
		Cursor rec;
		rec =db.rawQuery("select name from "+TABLE_BIRTH_ANN+ ";",null);
		
		rec.moveToFirst();
	
		return rec;
	}
}



n here's the code where i try to retrieve the db
Month.java
package com.pack.Project_Reminder;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

public class Month extends ListActivity {
    /** Called when the activity is first created. */
	int pos;
    @Override
    public void onCreate(Bundle s) {
        super.onCreate(s);
        
        System.out.print("...................heloooo");
        
        Database db1=new Database(getApplicationContext());
       
       Intent i=getIntent();
        String m=(String) i.getCharSequenceExtra("month");
        Log.d("ss","String is................................."+m);
        int month=Integer.parseInt(m);
        db1.open();

        Cursor c=db1.get_bdy(month);
        c.moveToFirst();
        final String[] mon = new String[c.getCount()];
         for(int j=0; j<mon.length; j++){
        	 mon[j] = c.getString(j+1);
        	 c.moveToNext();
         }
         
         ArrayAdapter<String> adp = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mon);
         setListAdapter(adp);
         c.close();
         db1.close();
         }
    }


Here is where i get to next activity, on clicking the month it gives Force Close.
Birthday.java
package com.pack.Project_Reminder;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class Birthday extends ListActivity  {

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     
        ListView lv = getListView();      
        LayoutInflater in = getLayoutInflater(); 
        ViewGroup header = (ViewGroup) in.inflate(R.layout.header, (ViewGroup)findViewById(R.id.linear), false);      
        lv.addHeaderView(header, null, false);
        
        
        String []item1 = new String[]{"Jan","Feb","Mar","Apr","May","June","July","August","Sept","Oct","Nov","Dec"};
        ArrayAdapter<String> adap1 = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,item1);
        setListAdapter(adap1);
        
        ImageView img = (ImageView)findViewById(R.id.image);
        img.setonclickListener(new View.onclickListener() {
			
			public void onclick(View v) {
				// TODO Auto-generated method stub
				Intent in = new Intent(getApplicationContext(),Birth_Add.class);
				startActivity(in);
			}
		});
    }
    
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Intent i=new Intent(getApplicationContext(),Month.class);
		String pos=String.valueOf(position);
		i.putExtra("month", pos);
		startActivity(i);
		}
}



and here's the error as shown in Logcat
07-03 17:17:41.945: E/AndroidRuntime(6144): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pack.Project_Reminder/com.pack.Project_Reminder.Month}: java.lang.IllegalStateException: get field slot from row 0 col 1 failed

and it points to the following line in Month.java
mon[j]=c.getString(j+1);

I tried specifiying the row no. for the cursor to move to but still it gives the same error.

Kindly help out!
Thank You In advance!!! :)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1