Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6666911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:52:41+00:00 2026-05-26T02:52:41+00:00

I’ve just started developing for Android. and i have some problems implementing an SQLite

  • 0

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

}

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:52:42+00:00Added an answer on May 26, 2026 at 2:52 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.