gpt4 book ai didi

java - Android FileoutputStream 空指针异常

转载 作者:行者123 更新时间:2023-11-29 15:10:55 24 4
gpt4 key购买 nike

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 i = 0; i < 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;
}

这段代码给了我这个异常(exception):java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)”我需要帮助..我做错了什么?第一次迭代的 LogCat:

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 to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a 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

最佳答案

您没有初始化 context 对象,这就是您得到 NullPointerException 的原因。

database_wrapper() 构造方法中初始化 context 对象。

database_wrapper(Context context) {

this.context = context;

u_db = new users_database(context);
sql_db = u_db.getWritableDatabase();
}

关于java - Android FileoutputStream 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31586576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com