android sqlite数据库,空指针

时间:2011-11-13 21:14:49

标签: java android database sqlite nullpointerexception

我正在尝试获取并将信息设置到我的数据库中,但它一直返回null。

我测试了它,我的DatabaseHelper类似乎有问题以及我是如何使用它的。这是我的DatabaseHelper课程。数据库本身已经使用sqlite数据库管理器创建,并且位于assets文件夹中。

public class DatabaseHelper extends SQLiteOpenHelper{

private static String DB_PATH = "/data/data/package_name/databases/";

private static String DB_NAME = "example.db";
private static int DB_VERSION = 1;

private SQLiteDatabase myDataBase; 

private final Context myContext;


public DatabaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.myContext = context;
}   


public void createDataBase() throws IOException{

    boolean dbExist = checkDataBase();

    if(dbExist){
    }else{


     with our database.
        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}


  private boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, 
    SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e){


    }

    if(checkDB != null){

        checkDB.close();

    }

    return checkDB != null ? true : false;
  }


  private void copyDataBase() throws IOException{

    InputStream myInput = myContext.getAssets().open(DB_NAME);

    String outFileName = DB_PATH + DB_NAME;

    OutputStream myOutput = new FileOutputStream(outFileName);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close();

}


    public void openDataBase() throws SQLException{

    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, 
   SQLiteDatabase.OPEN_READWRITE);


       }

   public void setStuff (String columns, String table, String values, String              
   whereclause){
    openDataBase();
    myDataBase.execSQL("INSERT INTO "
             + table
             + " "+columns
             + " VALUES ('"+values+")" + " WHERE "+whereclause);
    close();

   }

   public ArrayList<String> getStuff (String column, String table, String whereclause){
    ArrayList<String> stuffList = new ArrayList<String>();
    openDataBase();
    Cursor c = myDataBase.rawQuery("SELECT "+column +
            " FROM " +table
            + " WHERE "+whereclause,
            null);
    int index = c.getColumnIndex(column);
     if (c != null) {
         if (c.isFirst()) {
                 do {

                         String stuff = c.getString(index).toString();
                         results.add(data);
                 } while (c.getString(index)!=null);
         }
     }
     close();
     return stuffList;


}


   @Override
public synchronized void close() {

        if(myDataBase != null)
            myDataBase.close();

        super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {
    try {
        createDataBase();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}




@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


}

接下来我实际尝试使用这个数据库助手的类就是我的PersonDBController类,它在我的signUp活动中被使用,我只是用参数调用该方法。

        public class PersonDBController {
            private DatabaseHelper d;
            private Person p;   


                public String getFirstname(int userIDnum) {
                        ArrayList<String> fn = 
                                                      d.getStuff("firstname", 
                                                      "people", "userIDnum     
                                                       ="+userIDnum);
                        String result = fn.get(0);
                        return result;
                    }

这只是该课程中众多方法中的一种。但是,这不能正常工作。在活动中调用时 - 比如在按钮上单击以设置或获取内容它会返回空指针异常。

知道我做错了什么?

1 个答案:

答案 0 :(得分:-1)

您可能永远不会初始化d。检查你的代码,你有什么类似的东西吗?

d = new DatabaseHelper(this);

如果没有,那可能就是你发出的地方。

相关问题