2011-10-04 8 views
0

私はAndroid向けに開発を開始しました。私はSQLiteデータベースを実装するいくつかの問題があります。私はassestsからコピーされた既存のデータベースを使用します。ここに私のコードは次のとおりです。ここAndroid SQLite Nullポインタ例外既存のデータベースを使用

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); 
} 

は私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(dbExist){ 

    }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(); 

} 

スタックトレース:

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 
+0

getCarWithModel()を呼び出す前にCarsDBオブジェクトを初期化しましたか? –

+0

はい私はそれをしました。 CarsDB carDB =新しいCarsDB(これ); carDB.open(); カーソルカーソル=(カーソル)carDB.getCarWithModel( "Alfa"); – user977743

答えて

0

を参照してくださいCreate a database using existing database。この投稿には、既存のデータベースを使用してデータベースを作成して使用するためのコードスニペットが含まれています。

関連する問題