0

Hi i am trying to populate a list view I am using the following on my Main Acivity to connect my db handler

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

     List<Patients> patientsList= new ArrayList<Patients>();
     patientsList = _db.getALlPatients();
    ArrayAdapter<Patients> adapter = new ArrayAdapter<Patients>
    this,android.R.layout.simple_list_item_1,patientsList);
    ListView lv= (ListView) findViewById(R.id.listView);
     lv.setAdapter(adapter);
    }

My GetAllPatients() as follows

public List<Patients> getALlPatients() {
    List<Patients> _patients = new ArrayList<Patients>();

    SQLiteDatabase db = getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS, null);

    if (cursor.moveToFirst()) {
        do {
            _patients.add(new Patients(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),cursor.getString(5), Uri.parse(cursor.getString(6))));
        }
        while (cursor.moveToNext());
    }
    if (cursor.getCount()==0)
    {

        _patients.add(new Patients(1,"David Buckley","","","","",null));
        _patients.add(new Patients(2,"James Buckley","","","","",null));
    }

    cursor.close();
    db.close();
    return _patients;
}

Problem

As you see im using if (cursor.getCount()==0) to insert some test data as a test but when launching the app i am getting the following from the compiler:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.okason.Cautio/com.davidbuckley.Cautio.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.davidbuckley.Cautio.Dal.DatabaseHandler.getALlPatients()' on a null object reference

I persume this is something to do with write access for the db?. Which I am creating using the following

 public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," + KEY_PHONE + " TEXT," + KEY_EMAIL + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_IMAGEURI + " TEXT)");
    }

 public void createContact(Patients contact) {
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_NAME, contact.getName());
    values.put(KEY_PHONE, contact.getPhone());
    values.put(KEY_CONTACTPHONE, contact.get_contactPhone());

    values.put(KEY_EMAIL, contact.getEmail());
    values.put(KEY_ADDRESS, contact.getAddress());
    values.put(KEY_IMAGEURI, contact.getImageURI().toString());

    db.insert(TABLE_CONTACTS, null, values);
    db.close();
}

Anyone got any ideas and also how do i browse the db on the device is thier any method for checking the file structure ?.

Update I treid initiallizing the db with this.

List patientsList= new ArrayList();

    DatabaseHandler db = new DatabaseHandler(this);

   patientsList = db.getALlPatients();

 ArrayAdapter<Patients> adapter = new ArrayAdapter<Patients>(this,android.R.layout.simple_list_item_1,patientsList);

  ListView lv= (ListView) findViewById(R.id.listView);

  lv.setAdapter(adapter);

But now it just crashes on load with the same error.

Attempt to invoke virtual method 'java.util.List com.davidbuckley.Cautio.Dal.DatabaseHandler.getALlPatients()' on a null object refer

c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100
  • There are two problems here: the exception and browsing db. For browsing you can check the answer here http://stackoverflow.com/questions/2530548/browse-data-in-android-sqlite-database . For the exception, it doesn't appear that the error is related to db. It says something about mFragmentManager. I think you should post this part of the code. – inmyth Apr 24 '15 at 17:17
  • @inmyth sorry i pasted the wrong error silly me their is the correct error now getting used to java debugging mate – c-sharp-and-swiftui-devni Apr 24 '15 at 17:32

2 Answers2

0

In regards to browsing your db ( assuming you're using SQLite) you can use DBBrowser to check your table structures and contents. Although to do so you need to pull your applications DB file first and load it through DBBrowser. This is no problem when using a VM since you can just use ADM to pull it but pulling the DB file from a physical device will be a bit harder as you have to root if first to gain access to where the DB file of your application will be stored.

Here's a link to DBBrowser

Cai
  • 5,063
  • 3
  • 18
  • 23
0

Here's my take on the situation. You see this line

patientsList = _db.getALlPatients();

This _db hasn't been initialized anywhere before in onCreate.

I think you want to do is simply

List<Patients> patientsList = getALlPatients();

and you should really fix that typo.

inmyth
  • 8,880
  • 4
  • 47
  • 52
  • but u need the db its part of the dbhandler class inmyth does not make since at all – c-sharp-and-swiftui-devni Apr 24 '15 at 18:18
  • And you are doing it inside `getAllPatients` with `SQLiteDatabase db = getWritableDatabase();`. I don't see any use of db reference elsewhere but inside the scope of that method, at least in your code. That error of yours basically says that `getAllPatients` is called on null object reference which in this case `_db`. – inmyth Apr 24 '15 at 18:28
  • no i was delcaring it as private DatabaseHandler db; in my properties sections – c-sharp-and-swiftui-devni Apr 24 '15 at 18:33