nullponterException while retrieving from db through cursor

This is a discussion on nullponterException while retrieving from db through cursor within the Beginning Developers forums, part of the Android Development category; 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. ...

Results 1 to 3 of 3

Thread: nullponterException while retrieving from db through cursor

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

    nullponterException while retrieving from db through cursor

    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

  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_
    Moving to appropriate section.

  4. #3
    App Developer alostpacket's Avatar
    Join Date
    Jan 2011
    Location
    NY
    Posts
    240
    Thanked
    32 times
    Twitter
    alostpacket
    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

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. Cursor Randomly Jumps
    By G2 Forum User in forum HTC G2
    Replies: 1
    Last Post: 11-12-2010, 06:53 AM

Search tags for this page

android code cursor is null

,

android cursor null pointer exception

,

android cursor nullpointerexception

,
android getasset returns null
,

android getassets nullpointerexception

,

android getreadabledatabase nullpointerexception

,
code retrieve missed call in android
,
compilestatement nullpointerexc
,
cursor null erro
,

getassets().open nullpointerexception

,
getreadabledatabase(); return null
,

nullpointerexception when open sqlite

,
openorcreatedatabase null pointer exception
,
sqlite cursor returning null in android
,

uncaught handler: thread main exiting due to uncaught exception database

Click on a term to search our sites for related topics.
Android Forum