java - Null pointer exception whilst adding to the DB -
i'm developing simple bird-watching log app 1 of classes @ college , seem not able spot heck going wrong, after spending few days banging head against wall, suppose it'd make more sense ask fresh pair of eyes have look.
the app simple - have 2 activities, 1 listview, gets populated db (mainactivity) , another, takes arguments create new entry (addactivity). besides these have dbhelper (mysqlitehelper) handle db calls, simple class describe how object should (sighting), datasource class (sightingdatasource) handle facilitate calls helper , bunch of non-relevant resources (layouts, etc.) addactivity blows null pointer exceptions whilst calling addsighting method. whatever reason doesn't int typecast of bcount field, testing purposes i've set static int, whilst figure out exception portion of problem.
here's source: addactivity:
package com.vladislavtachev.cscb763; import android.app.listactivity; import android.os.bundle; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.edittext; public class addactivity extends listactivity { public sightingsdatasource datasource; private edittext locfld = null; private edittext bnamefld = null; private edittext bcountfld = null; private button addsightbtn = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_add); locfld = (edittext) findviewbyid(r.id.locfld); bnamefld = (edittext) findviewbyid(r.id.bnamefld); bcountfld = (edittext) findviewbyid(r.id.bcountfld); setcontentview(r.layout.activity_add); addsightbtn = (button) findviewbyid(r.id.addsightbtn); addsightbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { addsight(v); } }); } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public void addsight(view view){ long time = system.currenttimemillis()/1000l; datasource.addsighting(locfld.gettext().tostring(), time, bnamefld.gettext().tostring(),3 // integer.parseint(bcountfld.gettext().tostring()) ); // datasource.addsighting(sighting); setcontentview(r.layout.activity_main); } }
sightingdatasource
package com.vladislavtachev.cscb763; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import java.sql.sqlexception; import java.util.arraylist; import java.util.list; public class sightingsdatasource { //db info private sqlitedatabase database; private mysqlitehelper dbhelper; private string[] allcolumns = { mysqlitehelper.col_id, mysqlitehelper.col_location, mysqlitehelper.col_time, mysqlitehelper.col_bname, mysqlitehelper.col_bcount }; public sightingsdatasource(context context){ dbhelper = new mysqlitehelper(context); } public void open(){ database = dbhelper.getwritabledatabase(); } public void close(){ dbhelper.close(); } public void addsighting(string l, long time, string bname, int bcount){ contentvalues values = new contentvalues(); values.put(mysqlitehelper.col_location, l); values.put(mysqlitehelper.col_time, time); values.put(mysqlitehelper.col_bname, bname); values.put(mysqlitehelper.col_bcount, bcount); long insertid = database.insert(mysqlitehelper.table_sightings, null, values); cursor cursor = database.query(mysqlitehelper.table_sightings, allcolumns, mysqlitehelper.col_id + " = " + insertid, null, null, null, null); cursor.movetofirst(); sighting newsighting = cursortosighting(cursor); cursor.close(); } private sighting cursortosighting(cursor cursor){ sighting sighting = new sighting(); sighting.setid(cursor.getlong(0)); sighting.setlocation(cursor.getstring(1)); sighting.settime(cursor.getlong(2)); sighting.setbname(cursor.getstring(3)); sighting.setbcount(cursor.getint(4)); return sighting; } public list<sighting> getallsightings(){ list<sighting> sightings = new arraylist<sighting>(); cursor cursor = database.query(mysqlitehelper.table_sightings, allcolumns, null, null, null, null, null); cursor.movetofirst(); while (!cursor.isafterlast()) { sighting sighting = cursortosighting(cursor); sightings.add(sighting); cursor.movetonext(); } cursor.close(); return sightings; } }
mysqlitehelper
package com.vladislavtachev.cscb763; import android.database.sqlite.sqliteopenhelper; import android.database.sqlite.sqlitedatabase; import android.content.context; public class mysqlitehelper extends sqliteopenhelper{ public final static string db_name = "sightings.db"; public final static int db_ver = 1; public final static string table_sightings = "sightings"; public final static string col_id = "_id"; public final static string col_location = "_loc"; public final static string col_time = "_time"; public final static string col_bname = "_bname"; public final static string col_bcount = "_bcount"; private final static string db_create = "create table if not exists" + table_sightings + "(" + col_id + " integer primary key autoincrement, " + col_location + " text not null, " + col_time + " integer not null, " + col_bname + " text not null, " + col_bcount + " integer not null);"; public mysqlitehelper(context context){ super(context, db_name, null, db_ver); } @override public void oncreate(sqlitedatabase database){ database.execsql(db_create); } @override public void onupgrade(sqlitedatabase database, int oldver, int newver){ database.execsql("drop table if exists " + table_sightings); oncreate(database); } }
activity_add.xml layout
<relativelayout 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" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context="com.vladislavtachev.cscb763.addactivity" style="@android:style/theme.material"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="@string/lblbname" android:id="@+id/lblbname" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bnamefld" android:layout_below="@+id/lblbname" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparentright="true" android:layout_alignparentend="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="@string/lblbcount" android:id="@+id/lblbcount" android:layout_alignbottom="@+id/bcountfld" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="number" android:ems="10" android:id="@+id/bcountfld" android:layout_below="@+id/bnamefld" android:layout_alignright="@+id/bnamefld" android:layout_alignend="@+id/bnamefld" android:layout_toendof="@+id/lblbcount" android:layout_torightof="@+id/lblbcount" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="@string/lbllocation" android:id="@+id/lbllocation" android:layout_alignbottom="@+id/locfld" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/locfld" android:layout_below="@+id/bcountfld" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_toendof="@+id/lbllocation" android:layout_torightof="@+id/lbllocation" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/addsightbtn" android:id="@+id/addsightbtn" android:layout_centervertical="true" android:layout_centerhorizontal="true"/> <listview android:layout_width="10px" android:layout_height="10px" android:id="@android:id/list" android:layout_alignparentbottom="true" android:layout_alignright="@+id/locfld" android:layout_alignend="@+id/locfld" /> </relativelayout>
error stack
07-01 18:08:49.131 2384-2384/com.vladislavtachev.cscb763 e/androidruntime﹕ fatal exception: main process: com.vladislavtachev.cscb763, pid: 2384 java.lang.nullpointerexception: attempt invoke virtual method 'void com.vladislavtachev.cscb763.sightingsdatasource.addsighting(java.lang.string, long, java.lang.string, int)' on null object reference @ com.vladislavtachev.cscb763.addactivity.addsight(addactivity.java:75) @ com.vladislavtachev.cscb763.addactivity$1.onclick(addactivity.java:38) @ android.view.view.performclick(view.java:4780) @ android.view.view$performclick.run(view.java:19866) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
all appreciated!
cheers, v./
datasource
never initialized in addactivity
. call setcontentview
twice. delete second one.
Comments
Post a Comment