在android中查询数据库时获取空指针异常?

时间:2011-12-20 12:50:52

标签: android sqlite

我已经在android中的data/data folder中成功创建了表,但在尝试从数据库中获取值时,我仍然按照代码中的指示获取null pointer Exception。我检查了database但是列成功地从我的应用程序中获取值。快速帮助。

//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

1 个答案:

答案 0 :(得分: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;
        }
    }