Exception while creating android database

This is a discussion on Exception while creating android database within the Beginning Developers forums, part of the Android Development category; Here my code goes, but its not working. DBhelper.java public class DBHelper extends SQLiteOpenHelper { private Context myContext; private String DB_PATH = "/data/data/" + myContext.getApplicationContext().getPackageName() ...

Results 1 to 2 of 2

Thread: Exception while creating android database

  1. #1
    Android Lurker ahava's Avatar
    Join Date
    May 2011
    Posts
    2

    Unhappy Exception while creating android database

    Here my code goes, but its not working.

    DBhelper.java

    public class DBHelper extends SQLiteOpenHelper {

    private Context myContext; private String DB_PATH = "/data/data/" + myContext.getApplicationContext().getPackageName() + "/databases/";

    private static final String DATABASE_NAME = "MissedCallDB";

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_TABLE = "Messages";

    private SQLiteDatabase myDataBase;

    private SQLiteStatement insertStmt;
    private static final String INSERT = "insert into " + DATABASE_TABLE
    + " values(1, "
    + "'',"
    + "''"
    + "1"
    +");";


    public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.myContext = context;
    this.myDataBase = getWritableDatabase();
    this.insertStmt = this.myDataBase.compileStatement(INSERT);
    }

    public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
    // do nothing - database already exist
    } else {
    this.getReadableDatabase();

    try {

    copyDataBase();

    } catch (IOException e) {

    throw new Error("Error copying database");

    }
    }

    }

    private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

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

    } catch (SQLiteException e) {

    // database does't exist yet.

    }

    if (checkDB != null) {

    checkDB.close();

    }

    return checkDB != null ? true : false;
    }

    private void copyDataBase() throws IOException {

    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DATABASE_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DATABASE_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 + DATABASE_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
    SQLiteDatabase.OPEN_READWRITE);

    }

    @Override
    public synchronized void close() {
    if (myDataBase != null)
    myDataBase.close();

    super.close();

    }

    public SQLiteDatabase getDb() {
    return myDataBase;
    }

    public long insert(int index,String title,String message,int enabled) {

    this.insertStmt.bindString(1, title);
    this.insertStmt.bindString(2, message);
    this.insertStmt.bindLong(3, enabled);
    return this.insertStmt.executeInsert();
    }

    public void onCreate(SQLiteDatabase db) {
    String sql = "create table "+ DATABASE_TABLE +" (_id integer primary key autoincrement, "
    + "title text not null,"
    + "message text not null"
    + "enable integer"
    +");";
    Log.d("EventsData", "onCreate: " + sql);
    db.execSQL(sql);

    }

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

    }
    }

    From Actvity Missedcall.java DBHelper.java is evoked.

    Missedcall.java


    public class Missedcall extends ListActivity {

    private DBHelper dbHelper;
    private Cursor c;

    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.contact_list);

    dbHelper = new DBHelper(this);
    }
    }

    Errors:

    SQLiteDatabase db;
    private DBHelper dbHelper;
    private Cursor c;

    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.contact_list);

    dbHelper = new DBHelper(this);
    }
    }

    Errors:

    05-17 12:36:15.773: ERROR/AndroidRuntime(387): FATAL EXCEPTION: main
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): java.lang.RuntimeException: Unable to start activity ComponentInfo{Project.MCR/Project.MCR.Missedcall}: java.lang.NullPointerException
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2663)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2679)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at android.app.ActivityThread.access$2300(ActivityThr ead.java:125)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at android.app.ActivityThread$H.handleMessage(Activit yThread.java:2033)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at android.os.Handler.dispatchMessage(Handler.java:99 )
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at android.os.Looper.loop(Looper.java:123)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at android.app.ActivityThread.main(ActivityThread.jav a:4627)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at java.lang.reflect.Method.invokeNative(Native Method)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at java.lang.reflect.Method.invoke(Method.java:521)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:868)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:626)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at dalvik.system.NativeStart.main(Native Method)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): Caused by: java.lang.NullPointerException
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at Project.MCR.DBHelper.(DBHelper.java:34)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at Project.MCR.Missedcall.onCreate(Missedcall.java:33 )
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1047)
    05-17 12:36:15.773: ERROR/AndroidRuntime(387): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2627)


    Sorry for such a long code. i m new to android.

    can anyone help me out.
    thanks in advance
    Last edited by ahava; 05-17-2011 at 03:51 AM.

  2. Android.net is the premier Android Forum. Registered users do not see these ads. .

  3. #2
    Super Moderator Droid-Xer's Avatar
    Join Date
    Jan 2011
    Location
    El Paso, TX
    Posts
    2,239
    Thanked
    59 times
    Twitter
    _B0BB0_
    Unsure of your error. But let me move this to a better section for you.

Remove Ads

http://www.scramblerducati.org/

Sponsored Links

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Similar Threads

  1. Replies: 0
    Last Post: 03-14-2011, 12:41 PM
  2. Replies: 0
    Last Post: 02-28-2011, 12:39 PM
  3. Replies: 13
    Last Post: 01-26-2011, 09:12 PM

Search tags for this page

android database not creating

,

android error copying database

,
android getreadabledatabase() error
,

android getwritabledatabase nullpointerexception

,

android not creating database

,
android nullpointerexception getwritabledatabase();
,
android what cause getwritabledatabase() java.lang.nullpointerexception
,

database is not created in android

,
error/database 387 android
,

getwritabledatabase() nullpointerexception

,

java.lang.error error copying database

,

java.lang.error: error copying database

,
nullpointerexception getwritabledatabase
,
unable to start activity componentinfo oncreate(sqlitedatabase db)
,
unable to start activity componentinfo{com.activity/com.activity.visitactivity}: android.database.sqlite.sqliteexception
Click on a term to search our sites for related topics.
Android Forum