PDA

View Full Version : nullponterException while retrieving from db through cursor



ahava
05-18-2011, 03:51 AM
Hi,
i exactly don't know why null pointer exception is generated wherever the cursor is called..
i m not able to figure out the problem.

Here is my code,


MissedCall.java

public class Missedcall extends ListActivity {


private SQLiteDatabase db;
private DBHelper dbHelper;
private Cursor c;

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


db = openOrCreateDatabase(
"MissedCallDB"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null

);


dbHelper = new DBHelper(this);
try
{
dbHelper.createDataBase();
}
catch (IOException ioe)
{
throw new Error("Unable to create database");
}

FillData(); // nullpointer exception here

}

public void FillData() {

c = dbHelper.GetAllRows(); // nullpointer exception here

startManagingCursor(c);
ListAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.message_view,
c,
new String[] {"title"},
new int[] {R.id.col1}
);
setListAdapter(adapter);
}


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 Cursor GetAllRows() {
try {

// nullpointer exception here
return db.query(DATABASE_TABLE, new String[] {
"_id", "title", "message"}, null, null, null, null, null);

} catch (SQLException e) {
Log.e("Exception on query", e.toString());
return null;
}
}
}

error:

05-18 11:41:10.491: ERROR/AndroidRuntime(267): Uncaught handler: thread main exiting due to uncaught exception
05-18 11:41:10.521: ERROR/AndroidRuntime(267): java.lang.RuntimeException: Unable to start activity ComponentInfo{Project.MCR/Project.MCR.Missedcall}: java.lang.NullPointerException
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2496)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2512)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at android.app.ActivityThread.access$2200(ActivityThr ead.java:119)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1863)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at android.os.Handler.dispatchMessage(Handler.java:99 )
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at android.os.Looper.loop(Looper.java:123)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at android.app.ActivityThread.main(ActivityThread.jav a:4363)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at java.lang.reflect.Method.invoke(Method.java:521)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:860)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:618)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at dalvik.system.NativeStart.main(Native Method)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): Caused by: java.lang.NullPointerException
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at Project.MCR.DBHelper.GetAllRows(DBHelper.java:299)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at Project.MCR.Missedcall.FillData(Missedcall.java:60 )
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at Project.MCR.Missedcall.onCreate(Missedcall.java:54 )
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1047)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2459)
05-18 11:41:10.521: ERROR/AndroidRuntime(267): ... 11 more




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

can anyone help me out..

thanks in advance

Droid-Xer
05-18-2011, 06:10 AM
Moving to appropriate section.

alostpacket
05-19-2011, 05:50 AM
That's a fair amount of code to go over but my guess is that "db" is null on that line, and probably doesnt exist yet. I havent done the trick where you copy a pre-made db from assets, but I would imagine the problem is that you haven't completed the copy or created the db before you call db.GetAllRows()

hth