nullpointerexception - Android FileoutputStream NullPOinter Exception -
public class database_wrapper { users_database u_db; sqlitedatabase sql_db; context context; database_wrapper(context context) { u_db = new users_database(context); sql_db = u_db.getwritabledatabase(); } public long insert_data(list<name_holder> list) { contentvalues contentvalues = new contentvalues(); long error_code = 0; string filename; for(int = 0; < list.size(); i++) { filename = "test_image";//string.valueof(list.get(i).id); log.v("datadata",filename); if(list.get(i)._bmp == null) { log.v("datadata", "no image"); } try { bytearrayoutputstream bytes = new bytearrayoutputstream(); fileoutputstream fo; list.get(i)._bmp.compress(bitmap.compressformat.jpeg, 100, bytes); fo = context.openfileoutput(filename, context.mode_private); fo.write(bytes.tobytearray()); // remember close file output fo.close(); } catch (exception e) { e.printstacktrace(); log.v("datadata", e.tostring()); filename = "null"; } contentvalues.put(u_db.id, list.get(i).id); log.v("datadata1", string.valueof(list.get(i).id)); contentvalues.put(u_db.name, list.get(i).name); log.v("datadata2", list.get(i).name); contentvalues.put(u_db.distance, list.get(i).distance); log.v("datadata3", string.valueof(list.get(i).distance)); contentvalues.put(u_db.avatar, filename); log.v("datadata4", filename); contentvalues.put(u_db.age, list.get(i).age); log.v("datadata5", list.get(i).age); contentvalues.put(u_db.sex, list.get(i).sex); log.v("datadata6", list.get(i).sex); contentvalues.put(u_db.orient, list.get(i).orient); log.v("datadata7", string.valueof(list.get(i).orient)); long j = sql_db.insert(u_db.table_name, null, contentvalues); if (j < 0) error_code = j; int y = (int)j; log.v("datadata", string.valueof(y)); } return error_code; }
this code gives me exception: java.lang.nullpointerexception: attempt invoke virtual method 'java.io.fileoutputstream android.content.context.openfileoutput(java.lang.string, int)' on null object reference need help.. did wrong? logcat first itiration:
07-23 06:45:56.686 8796-8915/? v/datadata﹕ test_image 07-23 06:45:56.696 8796-8915/? v/datadata﹕ java.lang.nullpointerexception: attempt invoke virtual method 'java.io.fileoutputstream android.content.context.openfileoutput(java.lang.string, int)' on null object reference 07-23 06:45:56.696 8796-8915/? v/datadata1﹕ 2 07-23 06:45:56.696 8796-8915/? v/datadata2﹕ `h jvkv h h r 07-23 06:45:56.696 8796-8915/? v/datadata3﹕ 0 07-23 06:45:56.696 8796-8915/? v/datadata4﹕ null 07-23 06:45:56.696 8796-8915/? v/datadata5﹕ 90 07-23 06:45:56.696 8796-8915/? v/datadata6﹕ male 07-23 06:45:56.696 8796-8915/? v/datadata7﹕ 0 07-23 06:45:56.706 8796-8915/? v/datadata﹕ -1
you didn't initialize context
object thats why getting nullpointerexception
.
initialize context
object inside database_wrapper()
constructor method.
database_wrapper(context context) { this.context = context; u_db = new users_database(context); sql_db = u_db.getwritabledatabase(); }
Comments
Post a Comment