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