gpt4 book ai didi

java - 局部变量可能尚未初始化

转载 作者:行者123 更新时间:2023-12-01 06:46:19 24 4
gpt4 key购买 nike

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 = "contextsManager";

// Locations table name
private static final String TABLE_LOCATIONLABLES = "locationLables";

// LOCATIONLABLES Table Columns names
private static final String KEY_LOCID = "loc_id";
private static final String KEY_LOCNAME = "loc_name";

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

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
+ KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
+ ")";
db.execSQL(TABLE_LOCATIONLABLES);
}

这是说局部变量TABLE_LOCATIONLABLES可能尚未初始化?已经初始化了,怎么会出现这个错误呢?

最佳答案

问题出在你的代码本身

 // Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
+ KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
+ ")";
db.execSQL(TABLE_LOCATIONLABLES);
}

在 oncreate(db) 中,您再次声明了 TABLE_LOCATIONLABLES 并在同一行中使用它,这就是为什么您得到 Localvariable may not have beeninitialized

只需像这样重命名 oncreate(db) 中的字符串 TABLE_LOCATIONLABLES

    @Override
public void onCreate(SQLiteDatabase db) {
String mQuery = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
+ KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
+ ")";
db.execSQL(mQuery);
}

关于java - 局部变量可能尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11860337/

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