0

I have successfully created the table in data/data folder in android,but while trying to fetch values from database , I am keep getting null pointer Exception as pointed out in code.I checked the database but columns are successfully getting the values from my application.Kindly help.

//some class    
//row_idealweight and row_goal are the name of column in the table table_name.

    DietpointHelper db;
   Cursor c=db.getReadableDatabase().query( db.table_name,new String[]{db.row_idealweight,db.row_goal}, null, null, null, null, null);<---NULL POINTER EXCEPTION

    while(c.moveToNext())
       {
       et_ideal.setText(c.getString(c.getColumnIndex(db.row_idealweight)));
       et_goal.setText(c.getString(c.getColumnIndex(db.row_goal))) ;
       }
       db.close();

    //end of class..

    public class DietpointHelper extends  SQLiteOpenHelper{
            /** Called when the activity is first created. */
            //creating db
            private final static String db_name="Modify_Program";
            private final static int vs=1;

           //creating columns

           final String table_name="Modify_program";
           final String row_Primary="_id";
           final String row_Weight="col_weight";
           final String row_Gender="col_gender";
           final String row_Height="col_height"; 
           final String row_Birthday="col_birthdate";
           final String row_Current_Weight="col_currentWeight";
           final String row_goal="col_goal";
           final String row_idealweight="col_idealweight";

            //creating statement
        String database_create= "CREATE TABLE "+table_name+"("
                +row_Primary+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
                + row_Weight + " TEXT, "
                +row_Gender+" TEXT , "
                +row_Height+" TEXT , "
                +row_Birthday+" TEXT, "
                +row_Current_Weight+" TEXT, "
                +row_idealweight+" TEXT, "
                +row_goal+" TEXT "+");" ;


            public  void addData(Record rec)
            {
                SQLiteDatabase db=this.getReadableDatabase();

                ContentValues values=new ContentValues();

                values.put(row_Weight,rec.getWeight());
                values.put(row_Gender, rec.getCheck());
                values.put( row_Height, rec.getHeight());
                values.put(row_Birthday, rec.getBday());
                values.put(row_Current_Weight, rec.getWeight());
                values.put(row_goal,rec.getGoal()); 
                values.put(row_idealweight,rec.getIdealWeight());
                values.put(row_Primary,1);

                  db.insert(table_name,row_Weight, values); 
                }


            public DietpointHelper(Context context)
                   {
                    super(context, db_name, null,vs);
                    // TODO Auto-generated constructor stub
                }

            public void onCreate(SQLiteDatabase database) 
            {

                database .execSQL(database_create);

            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
            {
                // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " +table_name );
                  onCreate(db);
            }    

        }//class
2
  • where do you initialize your DietpointHelper db; Commented Dec 20, 2011 at 12:54
  • my bad.I forgot.thanks for pointing out. Commented Dec 20, 2011 at 12:58

1 Answer 1

1
//use these in ur Activity    
    DataBaseAdapter dbAdapter = new DataBaseAdapter(this);
                dbAdapter.open();

     Cursor cr = dbAdapter.Yourfetch_method Name();

                cr.moveToFirst();

        while (!cr.isAfterLast()) {
                map = new HashMap<String, Object>();

                byte[] bytes = cr.getBlob(cr.getColumnIndex("Image"));

                Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

                map.put("Image", bmp);

                map = null;     

                    cr.moveToNext();

                }
    //create table by using following class DbHelper

    public class DBHelper extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "TryOnDB";

        private static final int DATABASE_VERSION =1;

        // Database creation sql statement

        public static final String tablename= "create table dbname( Image BLOB );";



        public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        // Method is called during creation of the database
        @Override
        public void onCreate(SQLiteDatabase database) {
            database.execSQL(tablename);

        }

        // Method is called during an upgrade of the database, e.g. if you increase
        // the database version
        @Override
        public void onUpgrade(SQLiteDatabase database, int oldVersion,
                int newVersion) {
            Log.w(DBHelper.class.getName(),
                    "Upgrading database from version " + oldVersion + " to "
                            + newVersion + ", which will destroy all old data");
            database.execSQL("DROP TABLE IF EXISTS FavoriteTable");

            onCreate(database);
        }


            public boolean deleteDatabase(Context context) {
                return context.deleteDatabase(DATABASE_NAME);
            }




    }

    //use DataBaseAdpter Class to fetchdata

    public class DataBaseAdapter {

        // Database fields

        private Context context;
        private SQLiteDatabase database;
        private DBHelper dbHelper;

        public DataBaseAdapter(Context context) {
            this.context = context;
        }

        public DataBaseAdapter open() throws SQLException {
            dbHelper = new DBHelper(context);
            database = dbHelper.getWritableDatabase();
            return this;
        }

        public void close() {
            dbHelper.close();
        }


        public Cursor Yourfetch_method Name() {
            return database.query("FavoriteData", new String[] {"Image"}, null, null, null, null, null);
        }


        public void deleteTable(String tablename){
            database.execSQL("drop table if exists "+tablename+';');
        }
        public void createIndividualTable(String query){
            database.execSQL(query);
        }




        public void InsertPhotosData(FavoriteData photos) {
            ContentValues values = new ContentValues();
            values.put("photo", photos.Image);

            database.insert("PhotosData", null, values);

        }


        public ContentValues createContentValues(String category, String summary,
                String description) {
            ContentValues values = new ContentValues();

            return values;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.