android - Db Handler returning null and cannot create table? -
i have database class using connect sql lite database listview class stores context stuff such retreieving rows simple getpaitnets failling full class
public class databasehandler extends sqliteopenhelper { private static final int database_version = 1; private static final string database_name = "cautiomanager", table_contacts = "patient", key_id = "id", key_name = "name", key_phone = "phone", key_contactphone = "contactphone", key_email = "email", key_address = "address", key_imageuri = "imageuri"; public databasehandler(context context) { super(context, database_name, null, database_version); } @override 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)"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_contacts); oncreate(db); } 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(); } public patients getpatientsbyid(int id) { sqlitedatabase db = getreadabledatabase(); cursor cursor = db.query(table_contacts, new string[] { key_id, key_name, key_phone, key_email, key_address, key_imageuri }, key_id + "=?", new string[] { string.valueof(id) }, null, null, null, null ); if (cursor != null) cursor.movetofirst(); patients _patients = 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))); db.close(); cursor.close(); return _patients; } public void deletecontact(patients contact) { sqlitedatabase db = getwritabledatabase(); db.delete(table_contacts, key_id + "=?", new string[] { string.valueof(contact.getid()) }); db.close(); } public int getcontactscount() { sqlitedatabase db = getreadabledatabase(); cursor cursor = db.rawquery("select * " + table_contacts, null); int count = cursor.getcount(); db.close(); cursor.close(); return count; } public int updatepatients(patients _patients) { sqlitedatabase db = getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_name, _patients.getname()); values.put(key_phone, _patients.getphone()); values.put(key_contactphone, _patients.get_contactphone()); values.put(key_email, _patients.getemail()); values.put(key_address, _patients.getaddress()); values.put(key_imageuri, _patients.getimageuri().tostring()); int rowsaffected = db.update(table_contacts, values, key_id + "=?", new string[] { string.valueof(_patients.getid()) }); db.close(); return rowsaffected; } public list<patients> getallpatients() { list<patients> _patients = new arraylist<patients>(); sqlitedatabase db = getwritabledatabase(); cursor cursor = db.rawquery("select * " + table_contacts, null); if (cursor.movetofirst()) { { _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; } }
then in activity using declare instance of db
public databasehandler db = new databasehandler(this); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mnavigationdrawerfragment = (navigationdrawerfragment) getsupportfragmentmanager().findfragmentbyid(r.id.navigation_drawer); mtitle = gettitle(); // set drawer. mnavigationdrawerfragment.setup( r.id.navigation_drawer, (drawerlayout) findviewbyid(r.id.drawer_layout)); list<patients> patientslist= new arraylist<patients>(); 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 when compile , run on emulator returns follow null object error.
sorry folks here error.
attempt invoke virtual method 'void android.widget.listview.setadapter(android.widget.listadapter)' on null object reference
showing layout file fragment_patient_list
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.davidbuckley.cautio.fragments.patientslistfragment"> <!-- todo: update blank fragment layout --> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listview" android:layout_gravity="center" /> </framelayout>
Comments
Post a Comment