gpt4 book ai didi

android - 为什么我在 this.getWritableDatabase(); 上出现空指针异常,android?

转载 作者:行者123 更新时间:2023-11-29 21:40:44 25 4
gpt4 key购买 nike

异常发生在 SQLiteDatabase db = this.getWritableDatabase();提供我的数据库处理程序类

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;


// Database Name
private static final String DATABASE_NAME = "chat";

// Contacts table name
private static final String TABLE_CHAT = "chat_history";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TT = "tt";
private static final String KEY_TYPE = "type";
private static final String KEY_MSG = "message";

public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CHAT_TABLE = "CREATE TABLE " + TABLE_CHAT + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_TT + " TEXT," + KEY_TYPE + " TEXT,"
+ KEY_MSG + " TEXT" + ")";
db.execSQL(CREATE_CHAT_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_CHAT);

// Create tables again
onCreate(db);
}

// Adding new contact

public void addContact(String tt, String type, String msg) {
SQLiteDatabase db = this.getWritableDatabase();
System.out.println("tt "+tt);
System.out.println("msg "+msg);
System.out.println("type "+type);
ContentValues values = new ContentValues();
values.put(KEY_TT, tt); // Contact Name
values.put(KEY_TYPE, type); // Contact Phone Number
values.put(KEY_MSG, msg);

// Inserting Row
db.insert(TABLE_CHAT, null, values);
db.close(); // Closing database connection
}

// Getting All Contacts
public List<ChatHistory> getAllContacts(String T) {
List<ChatHistory> contactList = new ArrayList<ChatHistory>();
// Select All Query
System.out.println("database T$$$$$$$$$$$"+T);

String selectQuery = "SELECT * FROM " + TABLE_CHAT + " WHERE "+ KEY_TT+"="+"'"+T+"'";

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
ChatHistory chatHistory = new ChatHistory();
chatHistory.setID(Integer.parseInt(cursor.getString(0)));
chatHistory.settt(cursor.getString(1));
chatHistory.settype(cursor.getString(2));
chatHistory.setmsg(cursor.getString(3));
// Adding contact to list
contactList.add(chatHistory);
} while (cursor.moveToNext());
}

// return contact list
return contactList;
}
}

我一直在这条线上遇到异常 SQLiteDatabase db = this.getWritableDatabase();

它的空指针异常..我无法解决这个问题。请提出解决方案这是上下文的一些错误如何解决?

最佳答案

我认为您的数据库尚未创建,因为您正在执行错误的查询。你忘了在末尾添加分号。

你正在做的是:

String CREATE_CHAT_TABLE = "CREATE TABLE " + TABLE_CHAT + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_TT + " TEXT," + KEY_TYPE + " TEXT,"
+ KEY_MSG + " TEXT" + ")";

改用这个

String CREATE_CHAT_TABLE = "CREATE TABLE " + TABLE_CHAT + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_TT + " TEXT," + KEY_TYPE + " TEXT,"
+ KEY_MSG + " TEXT" + ");";

然后卸载您的应用并重试。

关于android - 为什么我在 this.getWritableDatabase(); 上出现空指针异常,android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17185360/

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