Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8299671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:14:21+00:00 2026-06-08T16:14:21+00:00

I have a DatabaseHandler class like this: public class DatabaseHandler extends SQLiteOpenHelper { private

  • 0

I have a DatabaseHandler class like this:

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "CaseDatabase";
private static final String TABLE_CASES = "cases";
private static final String KEY_ID = "id";
private static final String DATE = "date";
private static final String RIGHTFINGER = "rightFinger";
private static final String LEFTFINGER = "leftFinger";


public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

      String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CASES + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + DATE + " TEXT,"
                + RIGHTFINGER + " BLOB," + LEFTFINGER +" BLOB" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

      db.execSQL("DROP TABLE IF EXISTS " + TABLE_CASES);

        // Create tables again
        onCreate(db);

}

public void addContact(Case c) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_ID, c.getCaseNumber()); // PCN
    values.put(DATE, c.getDate()); // Date

    ByteArrayOutputStream out1 = new ByteArrayOutputStream();
    c.getLeftIndexFinger().compress(Bitmap.CompressFormat.PNG, 100, out1); 

    ByteArrayOutputStream out2 = new ByteArrayOutputStream();
    c.getRightIndexFinger().compress(Bitmap.CompressFormat.PNG, 100, out2); 

    values.put(LEFTFINGER, out1.toByteArray()); // LFINGER
    values.put("RIGHTFINGER", out2.toByteArray()); //RFINGER



    // Inserting Row
    db.insert(TABLE_CASES, null, values);
    db.close(); // Closing database connection
}

public ArrayList<Case> getAllContacts() {
    ArrayList<Case> caseList = new ArrayList<Case>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CASES;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Case c = new Case();

            c.setCaseNumber(cursor.getString(0));
            c.setDate(cursor.getString(1));

            byte[] blob1 = cursor.getBlob(cursor.getColumnIndexOrThrow("2"));
            Bitmap bmp1 = BitmapFactory.decodeByteArray(blob1, 0,blob1.length);

            c.setLeftIndexFinger(bmp1);

            byte[] blob2 = cursor.getBlob(cursor.getColumnIndexOrThrow("3"));
            Bitmap bmp2 = BitmapFactory.decodeByteArray(blob1, 0,blob2.length);
            c.setRightIndexFinger(bmp2); 


            // Adding contact to list
            caseList.add(c);
        } while (cursor.moveToNext());
    }

    // return contact list
    return caseList;
}

}

The add method works perfectly, but when I try to call the getAllContacts method, like this:

public ArrayList<Case> getFromDatabase() {

    ArrayList<Case> c = db.getAllContacts();    
    return c; 
}

list = getFromDatabase();

The app crashes and LogCat prints out following:

07-30 12:07:56.570: E/AndroidRuntime(13040): FATAL EXCEPTION: main

The method where I try to get records from the database, is in another activity than the add method

this is my getAllContacts method:

public ArrayList<Case> getAllContacts() {
    ArrayList<Case> caseList = new ArrayList<Case>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CASES;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Case c = new Case();

            c.setCaseNumber(cursor.getString(0));
            c.setDate(cursor.getString(1));

            byte[] blob1 = cursor.getBlob(cursor.getColumnIndexOrThrow("2"));
            Bitmap bmp1 = BitmapFactory.decodeByteArray(blob1, 0,blob1.length);

            c.setLeftIndexFinger(bmp1);

            byte[] blob2 = cursor.getBlob(cursor.getColumnIndexOrThrow("3"));
            Bitmap bmp2 = BitmapFactory.decodeByteArray(blob1, 0,blob2.length);
            c.setRightIndexFinger(bmp2); 


            // Adding contact to list
            caseList.add(c);
        } while (cursor.moveToNext());
    }

    // return contact list
    return caseList;
}

LogCat info:

  E/AndroidRuntime(15058): FATAL EXCEPTION: main
E/AndroidRuntime(15058): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package.hello/com.package.hello.History}: java.lang.NullPointerException
E/AndroidRuntime(15058):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
E/AndroidRuntime(15058):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
E/AndroidRuntime(15058):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
E/AndroidRuntime(15058):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
E/AndroidRuntime(15058):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(15058):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(15058):    at android.app.ActivityThread.main(ActivityThread.java:4517)
E/AndroidRuntime(15058):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(15058):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(15058):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
E/AndroidRuntime(15058):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
E/AndroidRuntime(15058):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(15058): Caused by: java.lang.NullPointerException
E/AndroidRuntime(15058):    at com.package.hello.History.getFromDatabase(History.java:153)
E/AndroidRuntime(15058):    at com.package.hello.History.onCreate(History.java:49)
E/AndroidRuntime(15058):    at android.app.Activity.performCreate(Activity.java:4533)
E/AndroidRuntime(15058):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
E/AndroidRuntime(15058):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)

The Line 153 in History.java is this:

    ArrayList<Case> c = db.getAllContacts();   

In the history class, the db object hasn’t been declared its because this class inherites from the class where the add method is.

EDIT 3

My history class

public class History extends SuperClass {

ListView lv;
ArrayList<Case> list; 
ArrayList<Case> tempCaseList; 
TextView header;
EditText ed; 
String[] tempList; 
int textLength; 
TextView error;
Boolean[] isPresent;
DatabaseHandler db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setTabBar(R.layout.history); 

    lv = (ListView)findViewById(R.id.listOfCases); 
    list = new ArrayList<Case>(); 

    list = getFromDatabase();

    lv.setAdapter(new CustomAdapter(this, list)); 
    lv.setTextFilterEnabled(true);

    header = (TextView)findViewById(R.id.textView1);
    header.setText("History");
    error = (TextView)findViewById(R.id.error);

    // db = new DatabaseHandler(this);


    tempList = getCaseNumberToTempList(list);
    tempCaseList = createTempList(list); 



    ed = (EditText)findViewById(R.id.editText1);

    ed.addTextChangedListener(new TextWatcher() {



        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {


        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

            textLength = ed.getText().length();
            list.clear(); 
            isPresent = initIsPresentArray(); 

            for (int i = 0; i < tempList.length; i++) {
                for (int j = 0; j < (tempList[i].length()-(textLength-1)); j++) {
                    if (textLength <= tempList[i].length()) {
                        if(isPresent[i] == false){
                            if (ed.getText().toString().equalsIgnoreCase((String) tempList[i].subSequence(j,(j+textLength)))) {
                                list.add(tempCaseList.get(i));
                                isPresent[i] = true;
                        }
                    }
                }
            }
            if(list.size() == 0) {
                error.setText("Not Found"); 
            }
            else {
                error.setText(""); 
            }

            lv.setAdapter(new CustomAdapter(History.this, list)); 
        }
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }
    });


}

public Boolean[] initIsPresentArray() {
    Boolean[] isPresent = new Boolean[tempCaseList.size()];
    for(int i = 0; i < isPresent.length; i++) {
        isPresent[i] = false; 
    }
    return isPresent; 
}

public ArrayList<Case> createTempList(ArrayList<Case> listOfCases) {

    ArrayList<Case> temporaryList = new ArrayList<Case>(); 

    for(Case c : listOfCases) {
        temporaryList.add(c);
    }

    return temporaryList;
}

public String[] getCaseNumberToTempList(ArrayList<Case> caseList) {

    String[] objectCaseNumber = new String[caseList.size()];  

    for(int i = 0; i < caseList.size(); i++) {
        objectCaseNumber[i] = caseList.get(i).getCaseNumber();  
    }

    return objectCaseNumber;

}


public ArrayList<Case> getFromDatabase() {

    ArrayList<Case> c = db.getAllContacts();    
    return c; 
}

 }

FINAL EDIT

Found my error..I just forgot the init. the db object before calling the method which uses this object. But I got another error, which is not the topic of this thread so I will post a new thread, if I get stuck again. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:14:28+00:00Added an answer on June 8, 2026 at 4:14 pm

    Following your edit, the code is crashing here:

    ArrayList<Case> c = db.getAllContacts();   
    

    The only thing here that could be causing a NullPointerException is db itself being null. You need to check that db has been correctly initialised at this point.


    My earlier answer, which is still valid:

    It’s hard to be sure without seeing the full stracktrace, but this definitely looks wrong:

    byte[] blob1 = cursor.getBlob(cursor.getColumnIndexOrThrow("2"));
    

    This is looking for a column named "2", which doesn’t exist. Since you already know the column index of the blob you’re looking for, you probably meant this:

    byte[] blob1 = cursor.getBlob(2);
    

    or alternatively, if you really do want to find the index by column name:

    byte[] blob1 = cursor.getBlob(cursor.getColumnIndexOrThrow("rightFinger"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a class call DatabaseHandler extending SQLiteOpenHelper. I want to explicting deleted
I have a base class that declares a private non-static reference to the DataBase
have written this little class, which generates a UUID every time an object of
have a php code like this,going to convert it in to C#. function isValid($n){
Why does this autoload class duplicate the db connection? class autoloader { private $directory_name;
have next code: class GameTexture { private: LPDIRECT3DTEXTURE9 texture; unsigned char *alphaLayer; UINT width,
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
Have converted devise new session from erb to Haml but doens't work, this is
have anyone can tell me what syntax error on this actionscript (actionscript3.0)? var rotY:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.