Android: NullPointerException Unable to load database into listview within a fragment -
i've been searching while trying find how populate listview information mysqlite database. when think found information come nullpointers.
here code (homefragment.java) :
package zygs.com.seniorproject.fragments; import android.app.fragment; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.view.view.onclicklistener; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.listview; import java.util.arraylist; import java.util.list; import zygs.com.seniorproject.r; import zygs.com.seniorproject.database.databasehandler; import zygs.com.seniorproject.database.inventory; public class homefragment extends fragment { // initializing variables button item_button; button display_button; edittext box_item_des; edittext box_item_quant; edittext box_item_id; final databasehandler db = new databasehandler(getactivity()); public homefragment(){} @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { sethasoptionsmenu(true); final view rootview = inflater.inflate(r.layout.fragment_home, container, false); listview listcontent = (listview)rootview.findviewbyid(r.id.inventory_list); item_button = (button)rootview.findviewbyid(r.id.add_item); display_button = (button)rootview.findviewbyid(r.id.display_button); box_item_des = (edittext)rootview.findviewbyid(r.id.item_des); box_item_id = (edittext)rootview.findviewbyid(r.id.item_id); box_item_quant = (edittext)rootview.findviewbyid(r.id.item_quant); item_button.setonclicklistener( new view.onclicklistener() { public void onclick(view view) { // grab information text boxes string item_des = box_item_des.gettext().tostring(); int item_quantity = integer.parseint(box_item_quant.gettext().tostring()); int item_id = integer.parseint(box_item_id.gettext().tostring()); db.additem(new inventory(item_id, item_des, item_quantity)); } } ); display_button.setonclicklistener( new onclicklistener() { public void onclick(view view){ log.d("reading: ", "reading inventory"); list<inventory> inventorylist = db.getallitems(); (inventory in : inventorylist) { string log = "id: " + in.getitemid() + ", name: " + in.getitemname() + " , quantity: " + in.getitemquant(); //write list log log.d("name:", log); } } } ); list<inventory> inv_list = getallinventory(); arrayadapter adapter = new arrayadapter(getactivity(), android.r.layout.simple_list_item_1, inv_list); listcontent.setadapter(adapter); return rootview; } public list<inventory> getallinventory() { list<inventory> contactlist = new arraylist<inventory>(); string selectquery = "select * inventory"; sqlitedatabase data = db.getwritabledatabase(); cursor cursor = data.rawquery(selectquery, null); if (cursor.movetofirst()) { { inventory inventory = new inventory(); inventory.setitemid(integer.parseint(cursor.getstring(0))); inventory.setitemname(cursor.getstring(1)); inventory.setitemquant(integer.parseint(cursor.getstring(2))); } while (cursor.movetonext()); } return contactlist; } }
i using database handler androidhive
here's code:
package zygs.com.seniorproject.database; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import java.util.arraylist; import java.util.list; public class databasehandler extends sqliteopenhelper { //logcat tag private static final string log = "databasehelper"; //database version private static final int database_version = 2; //database name private static final string database_name = "inventorymanager"; //tables private static final string table_inventory = "inventory"; private static final string table_kits = "kits"; // common keys private static final string key_item_id = "item_id"; //kits table columns name private static final string key_id = "_id"; private static final string key_kit_id = "kit_id"; private static final string key_kit_name = "kit_name"; //inventory tables columns names private static final string key_item_name = "item_name"; private static final string key_item_quant = "item_quant"; public databasehandler(context context) { super(context, database_name, null, database_version); } //creating tables strings private static final string create_inventory_table = "create table " + table_inventory + "(" + key_item_id + " integer primary key," + key_item_name + " text," + key_item_quant + " integer" + ");"; private static final string create_kit_table = "create table " + table_kits + "(" + key_id + " autoincrement integer primary key," + key_kit_id + " integer," + key_kit_name + " text," + key_item_id + " integer" + ");"; //create tables @override public void oncreate(sqlitedatabase db) { db.execsql(create_inventory_table); db.execsql(create_kit_table); } //upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { //drop older table if existed db.execsql("drop table if exists " + table_inventory); // create tables again; oncreate(db); } ///////////////////////////////////////// /////// create read update delete ////// /////////////////////////////////////// //add new record public void additem(inventory inventory) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_item_name, inventory.getitemname()); // item name values.put(key_item_id, inventory.getitemid()); // item id values.put(key_item_quant, inventory.getitemquant()); //item quantity //insert row db.insert(table_inventory, null, values); db.close(); } // query single item //public inventory getitemid(int id) {} //query items public list<inventory> getallitems() { list<inventory> inventorylist = new arraylist<inventory>(); // select query string selectquery = "select * " + table_inventory; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); //looping through rows , adding list if (cursor.movetofirst()) { { inventory inventory = new inventory(); inventory.setitemid(integer.parseint(cursor.getstring(0))); inventory.setitemname(cursor.getstring(1)); inventory.setitemquant(integer.parseint(cursor.getstring(2))); //adding inventory list inventorylist.add(inventory); } while (cursor.movetonext()); } //return inventory list return inventorylist; } // query item count //public int getitemcount() {} //update single item //public int updateitem(inventory inventory) {} //delete single item //public void deleteitem(inventory inventory){} }
the error log goes followed:
04-09 13:34:54.187 6393-6393/zygs.com.seniorproject e/androidruntime﹕ fatal exception: main process: zygs.com.seniorproject, pid: 6393 java.lang.runtimeexception: unable start activity componentinfo{zygs.com.seniorproject/zygs.com.seniorproject.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'android.database.sqlite.sqlitedatabase android.content.context.openorcreatedatabase(java.lang.string, int, android.database.sqlite.sqlitedatabase$cursorfactory, android.database.databaseerrorhandler)' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2693) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2758) @ android.app.activitythread.access$900(activitythread.java:177) @ android.app.activitythread$h.handlemessage(activitythread.java:1448) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:145) @ android.app.activitythread.main(activitythread.java:5942) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1399) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1194) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.database.sqlite.sqlitedatabase android.content.context.openorcreatedatabase(java.lang.string, int, android.database.sqlite.sqlitedatabase$cursorfactory, android.database.databaseerrorhandler)' on null object reference @ android.database.sqlite.sqliteopenhelper.getdatabaselocked(sqliteopenhelper.java:223) @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:163) @ zygs.com.seniorproject.fragments.homefragment.getallinventory(homefragment.java:97) @ zygs.com.seniorproject.fragments.homefragment.oncreateview(homefragment.java:83) @ android.app.fragment.performcreateview(fragment.java:2114) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:904) @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1082) @ android.app.backstackrecord.run(backstackrecord.java:833) @ android.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1467) @ android.app.activity.performstart(activity.java:6328) @ android.app.activitythread.performlaunchactivity(activitythread.java:2656) at android.app.activitythread.handlelaunchactivity(activitythread.java:2758) at android.app.activitythread.access$900(activitythread.java:177) at android.app.activitythread$h.handlemessage(activitythread.java:1448) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:145) at android.app.activitythread.main(activitythread.java:5942) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372)
thank can get.
the line final databasehandler db = new databasehandler(getactivity());
declared @ top of class , on creation of fragment getactivity() return null reference.
if move inside oncreate() method have activity reference db handler can use.