Local variable may not have been initialized

蹲街弑〆低调 提交于 2020-01-17 03:57:05

问题


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);   
}

It is saying local variable TABLE_LOCATIONLABLES may not have been initialized? It is initialized how is this error happening?


回答1:


probelm lies withing your code itself

 // 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);   
    }

in oncreate(db) you have declared TABLE_LOCATIONLABLES again and using it in same line this is why you are getting Local variable may not have been initialized

just rename String TABLE_LOCATIONLABLES inside your oncreate(db) like this

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



回答2:


You are accessing TABLE_LOCATIONLABLES to create the String. The local variable has the same name as the static one so the local one gets selected. You should rename the local variable to solve this problem.




回答3:


You are declaring TABLE_LOCATIONLABLES twice. Once at class level, once inside the onCreate routine. Just remove the declaration inside the onCreate routine.

SCRAP THIS. I've just seen the answer below. Completely missed the fact that your class declaration was a static field.




回答4:


You have:

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

Note that you mention TABLE_LOCATIONLABLES twice there! While it may be bad style to re-use a static member name, a quick fix would be:

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

(PS: it's spelt LABELS, not LABLES!)




回答5:


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



回答6:


In OnCreate() method you are trying to access a variable which is a instance variable, but it is actually accessing the local method variable only. I suggest you to update your code as follows, so it will surely work,

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



回答7:


You have one static variable called TABLE_LOCATIONLABLES. In your onCreate method you are initalizing a variable with the same name - no problem until now. But in your statement String TABLE_LOCATIONLABLES = "CREATE TABLE " + TABLE_LOCATIONLABLES + "(" + KEY_LOCID + " INTEGER PRIMARY KEY," + KEY_LOCNAME + " TEXT," + ")";¸ you are accessing the local variable TABLE_LOCATIONLABLES! Though you are initalizing your local variable while assinging it within your statement, where at this moment the local variable isn't initalized.

I think you tried to access your static variable therefore you have to access it like DatabaseHandler.TABLE_LOACATIONLABLES.

Replace your old statement with this: String TABLE_LOCATIONLABLES = "CREATE TABLE " + DatabaseHandler.TABLE_LOCATIONLABLES + "(" + DatabaseHandler.KEY_LOCID + " INTEGER PRIMARY KEY," + DatabaseHandler.KEY_LOCNAME + " TEXT," + ")";¸

A small hint: Access your variables if they are members or static via this and so it's easier to see where the variables are instantiated.

Greets



来源:https://stackoverflow.com/questions/11860337/local-variable-may-not-have-been-initialized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!