android - nullPointerException on SQLiteDatabase db = dbHelper.getWritableDatabase(); insert method of ContentProvider -
i got nullpointerexception on line, listview.setadapter(adapter); in main activity class, cannot figure out causing it, ideas?
oncreate method of mainactivity class
public class mainactivity extends activity implements loadermanager.loadercallbacks<cursor>{ private simplecursoradapter adapter; private loadermanager loadermanager; private cursorloader cursorloader; private listview listview; private int primarykey; private string primarykeystring; private string testtitle; private string testtext; private static final int loader_id = 1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); primarykey = 0; testtitle = "test title"; testtext = "test text"; primarykeystring = string.valueof(primarykey); getloadermanager().initloader(loader_id, null, this); adapter = new simplecursoradapter(this, r.layout.row_layout, null, new string[]{primarykeystring, testtitle, testtext}, new int[]{r.id.textview1, r.id.textview2, r.id.textview3}, adapter.no_selection); listview = (listview) findviewbyid(r.id.listview1); listview.setadapter(adapter); // load database test values loaddatabase(); } // load database test data public void loaddatabase(){ uri mnewuri; contentvalues mnewvalues = new contentvalues(); mnewvalues.put(providerexample.noteitems.title, "test title"); mnewvalues.put(providerexample.noteitems.text, "test text value"); mnewuri = getcontentresolver().insert(providerexample.noteitems.content_uri, mnewvalues); }
the content provider class
public class providerexample extends contentprovider { public static final string database_name = "notes.db"; public static final int database_version = 1; public static final string table_name = "notes"; public static final string authority = "com.example.contentproviderexample.providerexample"; public static final urimatcher surimatcher; private static final int notes = 1; private static final int notes_id = 2; private static hashmap<string, string> notesprojectionmap; static { surimatcher = new urimatcher(urimatcher.no_match); surimatcher.adduri(authority, table_name, notes); surimatcher.adduri(authority, table_name + "/#", notes_id); } public static interface noteitems extends basecolumns { // notes in content_uri plural actual table name singular version of word public static final uri content_uri = uri.parse("content://" + authority + "/notes"); public static final string _id = "_id"; public static final string title = "title"; public static final string text = "text"; public static final string content_type = contentresolver.cursor_dir_base_type + "/vnd.example.providerexample"; // static final string single_record = "vnd.android.cursor.item/vnd.example.providerexample"; public static final string content_item_type = contentresolver.cursor_item_base_type + "/vnd.example.providerexample"; // static final string multiple_records = "vnd.android.cursor.dir/vnd.example.providerexample"; public static final string[] projection_all = {_id, title, text}; public static final string sort_order_default = title + " asc"; } // end lentitems interface private static class databasehelper extends sqliteopenhelper{ databasehelper(context context){ super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table if not exists " + table_name + " (" + noteitems._id + "id integer primary key autoincrement, " + noteitems.title + " text, " + noteitems.text + " text);"); } @override public void onupgrade(sqlitedatabase db, int previousversion, int newversion) { db.execsql("drop table if exists " + table_name); oncreate(db); } } private databasehelper dbhelper; @override public uri insert(uri uri, contentvalues initialvalues) { if(surimatcher.match(uri)!=notes){ throw new illegalargumentexception("unknown uri" + uri); } contentvalues values; if(initialvalues!=null){ values = new contentvalues(initialvalues); }else{ values = new contentvalues(); } sqlitedatabase db = dbhelper.getwritabledatabase(); // <--- nullpointerexception here long rowid = db.insert(table_name, null, values); if(rowid > 0){ uri noteuri = contenturis.withappendedid(noteitems.content_uri, rowid); getcontext().getcontentresolver().notifychange(noteuri, null); return noteuri; } throw new sqlexception("failed insert row " + uri); }
edit: nullpointerexception has changed sqlitedatabase db = dbhelper.getwritabledatabase(); of insert method
stakctrace:
androidruntime(19638): fatal exception: main androidruntime(19638): java.lang.runtimeexception: unable start activity componentinfo{com.example.contentproviderexample/com.example.contentproviderexample.mainactivity}: java.lang.nullpointerexception androidruntime(19638): caused by: java.lang.nullpointerexception androidruntime(19638): @ com.example.contentproviderexample.providerexample.insert(providerexample.java:104) androidruntime(19638): @ android.content.contentprovider$transport.insert(contentprovider.java:189) androidruntime(19638): @ android.content.contentresolver.insert(contentresolver.java:730) androidruntime(19638): @ com.example.contentproviderexample.mainactivity.loaddatabase(mainactivity.java:76) /androidruntime(19638): @ com.example.contentproviderexample.mainactivity.oncreate(mainactivity.java:47)
hard tell without stacktrace, seems null pointer isn't adapter, listview. looks haven't called setcontentview(), findviewbyid() method returning null, , you're trying set adapter null object.
Comments
Post a Comment