2

can you help me with this one. I don't know why I am getting null pointer exception even I know that the data is exist in the database. What I am trying to do is to access a method in a class, that method is for getting all the information of the given report code in the database. But it keeps on giving me null pointer exception. Here is the method I am referring to:

  • I am calling the method of different class through this:

    S_9th_ISubmit a = new S_9th_ISubmit();

    a.Uploading();

  • This is the method I am calling

    public void Uploading()
    {
    
    for (int i = 0; i < Constants.ARRAYLIST_REPORTCODES.size(); i++) 
    {
        Log.d("size:", String.valueOf(Constants.ARRAYLIST_REPORTCODES.size()));
        Log.d("content", String.valueOf(Constants.ARRAYLIST_REPORTCODES));
    
        final String code = Constants.ARRAYLIST_REPORTCODES.get(i).toString();
        Log.d("Uploading contents of Report Code: ", code);
    
        Cursor details = databaseHandler.getHeaderDetails(code);
    
    
        final String infotype   = details.getString(details.getColumnIndex(Constants.REPORT_INFOTYPECODE));
        String district         = details.getString(details.getColumnIndex(Constants.REPORT_DISTRICTCODE));
        final String province   = details.getString(details.getColumnIndex(Constants.REPORT_PROVINCECODE));
        final String date       = details.getString(details.getColumnIndex(Constants.REPORT_DATEOBSERVED));
        final String competitor = details.getString(details.getColumnIndex(Constants.REPORT_ISCOMPETITOR));
        final String remarks    = details.getString(details.getColumnIndex(Constants.REPORT_REMARKS));
    

    }

Do i need to open the database to access the database.? Below shows my logcat.

09-12 08:43:52.250: E/AndroidRuntime(14919): FATAL EXCEPTION: main
09-12 08:43:52.250: E/AndroidRuntime(14919): java.lang.NullPointerException
09-12 08:43:52.250: E/AndroidRuntime(14919):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
09-12 08:43:52.250: E/AndroidRuntime(14919):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
09-12 08:43:52.250: E/AndroidRuntime(14919):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:223)
09-12 08:43:52.250: E/AndroidRuntime(14919):    at com.dfd.ireport.DatabaseHandler.getHeaderDetails(DatabaseHandler.java:437)
09-12 08:43:52.250: E/AndroidRuntime(14919):    at com.dfd.ireport.S_9th_ISubmit.Uploading(S_9th_ISubmit.java:1137)

Here is the code in my databaseHandler

public Cursor getHeaderDetails(String code){
    SQLiteDatabase dbSqlite = this.getReadableDatabase();
    Cursor c = dbSqlite.query(Constants.TABLE_REPORT, new String[] { Constants.REPORT_ID, 
            Constants.REPORT_CODE, 
            Constants.REPORT_INFOTYPECODE, 
            Constants.REPORT_DISTRICTCODE, 
            Constants.REPORT_PROVINCECODE, 
            Constants.REPORT_DATEOBSERVED,
            Constants.REPORT_ISCOMPETITOR, 
            Constants.REPORT_REMARKS}, Constants.REPORT_CODE+"=?", new String[]{code}, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    dbSqlite.close();
    return c;
}

I tried to add this on my Uploading method to access the database but nothing happened.What else do I need to do, how can I check the context that I'm getting?

 DatabaseHandler databaseHandler;
        databaseHandler = new DatabaseHandler(this);
        if (databaseHandler != null)
        {
            databaseHandler.close();
            databaseHandler.createDB();
        }
7
  • What is on line 1137 of S_9th_ISubmit.java? Commented Sep 12, 2013 at 0:51
  • show code DatabaseHandler.getHeaderDetails() Commented Sep 12, 2013 at 0:51
  • this the content of the line 1137 Cursor details = databaseHandler.getHeaderDetails(code); Commented Sep 12, 2013 at 0:53
  • what is at line 437 in DatabaseHandler? Commented Sep 12, 2013 at 0:56
  • make sure you are not passing null to getHeaderDetails. Log and see what is code Commented Sep 12, 2013 at 0:57

2 Answers 2

3

My guess is in SQLiteDatabase dbSqlite = this.getReadableDatabase();, dbSqlite is null! getReadableDatabase() is returning null because you are passing a wrong context.
Try to check the context you are passing.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for giving me nice idea. I got an error because of wrong context.
0

Cursor details = databaseHandler.getHeaderDetails(code) show null point.

The problem is that the databaseHandler is null,you can check it with

Cursor details;
if(databaseHandler != null){
    details = databaseHandler.getHeaderDetails(code);
}else{
    Log.e(" databaseHandler : " ," databaseHandler == null");
}

if the problem in getHeaderDetails(),logcat will show the the line of code which cause null point exception,but i do not see

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.