PDA

View Full Version : Exception while creating android database



ahava
05-17-2011, 02:47 AM
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

Droid-Xer
05-17-2011, 04:22 AM
Unsure of your error. But let me move this to a better section for you.