局部变量可能尚未初始化

时间:2012-08-08 08:11:40

标签: java android sqlite compiler-errors local-variables

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可能尚未初始化?它初始化了这个错误是怎么发生的?

7 个答案:

答案 0 :(得分:4)

问题在于你的代码本身

 // 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并在同一行中使用它,这就是您获得Local variable may not have been initialized

的原因

只需在你的oncreate(db)中重命名String 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);   
    }

答案 1 :(得分:3)

您正在访问TABLE_LOCATIONLABLES以创建字符串。局部变量与静态变量具有相同的名称,因此本地变量被选中。您应该重命名局部变量以解决此问题。

答案 2 :(得分:2)

您要两次声明TABLE_LOCATIONLABLES。一旦进入班级,一旦进入onCreate例程。只需删除onCreate例程中的声明。

SCRAP THIS。我刚才看到了答案。完全错过了你的类声明是一个静态字段的事实。

答案 3 :(得分:1)

你有:

String TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "("
     /*^^^^^^^^^^^^^^^^^^^^*/                 /*^^^^^^^^^^^^^^^^^^^^*/
        + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
        +  ")";

请注意,您在那里提到TABLE_LOCATIONLABLES两次!虽然重复使用静态成员名称可能是不好的方式,但快速解决方法是:

String TABLE_LOCATIONLABLES = "CREATE TABLE " + DatabaseHandler.TABLE_LOCATIONLABLES + "("
                                              /*^^^^^^^^^^^^^^^^*/
        + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT,"
        +  ")";

(PS:拼写为LABELS,而不是LABLES!)

答案 4 :(得分:0)

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

答案 5 :(得分:0)

OnCreate()方法中,您尝试访问作为实例变量的变量,但它实际上只访问本地方法变量。我建议你按如下方式更新你的代码,这样肯定会有用,

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

答案 6 :(得分:0)

您有一个名为 TABLE_LOCATIONLABLES 静态变量。在 onCreate 方法中,您正在使用相同的名称进行变形 - 直到现在都没有问题。但在你的陈述中String TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "(" + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT," + ")";¸ 您正在访问本地变量TABLE_LOCATIONLABLES !虽然您在声明中声明了本地变量,但此时局部变量尚未初始化。

我认为您尝试访问静态变量,因此您必须像 DatabaseHandler.TABLE_LOACATIONLABLES 一样访问它。

用以下内容替换旧语句: String TABLE_LOCATIONLABLES = "CREATE TABLE " + DatabaseHandler.TABLE_LOCATIONLABLES + "(" + DatabaseHandler.KEY_LOCID + " INTEGER PRIMARY KEY," + DatabaseHandler.KEY_LOCNAME + " TEXT," + ")";¸

一个小提示:如果变量是成员或通过此变量静态访问变量,那么更容易看到变量实例化的位置。

迎接