开发者

Android SQLite Null Pointer Exception using existing database

开发者 https://www.devze.com 2023-04-10 04:23 出处:网络
I\'ve just started developing for Android. and i have some problems implementing an SQLite database. I use an existing database copied from assests . here is my code:

I've just started developing for Android. and i have some problems implementing an SQLite database. I use an existing database copied from assests . here is my code:

public class CarsDB {

private static final int VERSION_BDD = 1;
private static final String NOM_BDD = "cars.db";

private static final String TABLE = "Models";
private static final String COL_ID = "_id";
private static final int NUM_COL_ID = 2;
private static final String COL_MODEL = "Model";
private static final int NUM_COL_MODEL = 0;
private static final String COL_CONSO = "Conso";
private static final int NUM_COL_CONSO = 1;
private Context context;
private SQLiteDatabase db;

private DataBaseHelper SQLite;

public CarsDB(Context context){
    this.context=context;

    //SQLite = new DataBaseHelper(context);
}

public CarsDB open() throws SQLException{
    SQLite=new DataBaseHelper(context);
    //SQLite.close();
    db = SQLite.getWritableDatabase();
    return this;
}

public void close(){

    SQLite.close();
}

public SQLiteDatabase getBDD(){
    return db;
}
    public Cursor getCarWithModel(String Model){
    String loadFav = "SELECT * FROM Models where Model LIKE'"+Model+"%'";
    //Cursor c = db.query(TABLE, null,"Model LIKE'"+Model+"%'" , null, null, null, null);
    Cursor c = db.rawQuery(loadFav, null);
    return c;
    //return cursorToCar(c);
}

here is my OpenHelper :

public class DataBaseHelper extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/com.fresh.watch/databases/";
private static String DB_NAME = "cars.db";
private SQLiteDatabase myDataBase;
private final Context myContext;


public DataBaseHelper(Context context){
    super(context, DB_NAME,null,1);
    this.myContext=context;
}
public void createDataBase() throws IOException{
    boolean dbExist = checkDataBase();
    if(db开发者_StackOverflowExist){

    }else{

        this.getReadableDatabase();
        this.close();
        try {
            copyDataBase();
        } catch (IOException e) {
            // TODO: handle exception
            throw new Error("Error copying database");
        }
    }

}
private boolean checkDataBase() {
        // TODO Auto-generated method stub
        SQLiteDatabase checkDB = null;
        try {
            String myPath = DB_PATH + DB_NAME;
            //String myPath = Environment.getExternalStorageDirectory()+"/" + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {
            // TODO: handle exception

        }
        if(checkDB != null){
            checkDB.close();
        }
        return checkDB != null? true : false;
    }
private void copyDataBase() throws IOException{
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    //String outFileName = Environment.getExternalStorageDirectory()+"/" + DB_NAME;
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}
public void openDataBase() throws SQLException{

    //Open the database
    String myPath = DB_PATH + DB_NAME;
    //String myPath = Environment.getExternalStorageDirectory()+"/" + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}
public synchronized void close() {

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

    super.close();

}

Stack Trace :

    10-04 03:27:46.154: ERROR/AndroidRuntime(23348): FATAL EXCEPTION: main
10-04 03:27:46.154: ERROR/AndroidRuntime(23348): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fresh.watch/com.fresh.watch.Carmodels}: java.lang.NullPointerException
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at android.app.ActivityThread.access$1500(ActivityThread.java:123)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at android.os.Looper.loop(Looper.java:123)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at android.app.ActivityThread.main(ActivityThread.java:3835)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at java.lang.reflect.Method.invokeNative(Native Method)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at java.lang.reflect.Method.invoke(Method.java:507)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at dalvik.system.NativeStart.main(Native Method)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348): Caused by: java.lang.NullPointerException
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at com.fresh.watch.CarsDB.getCarWithModel(CarsDB.java:76)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at com.fresh.watch.Carmodels.onCreate(Carmodels.java:28)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
10-04 03:27:46.154: ERROR/AndroidRuntime(23348):     ... 11 more


See Create a database using existing database. This post contains a code snippet to create and use the database by using the existing database.

0

精彩评论

暂无评论...
验证码 换一张
取 消