简体   繁体   English

SQLite数据库获取所有记录NullPointerException

[英]SQLite Database Get All Records NullPointerException

The SQLite Database works fine as far as add/remove a single row until I added the getAllRecords Method and have it run in Oncreate. 只要添加/删除单行,SQLite数据库就可以正常工作,直到我添加了getAllRecords方法并使其在Oncreate中运行。

I've spent a lot of time fixing it, but no luck. 我花了很多时间修复它,但是没有运气。

Can someone please tell me what I am doing wrong? 有人可以告诉我我做错了吗?

MySQLite_Database: MySQLite_Database:

    public static final String KEY_ID = "ROW_ID";                   // (0)
    public static final String KEY_ADD_REMOVE = "AddorRemove";      // (1)
    public static final String GROUP_POS = "Group_Position";        // (2)
    public static final String CHILD_POS = "Child_Position";        // (3)
    public static final String DATABASE_TABLE = "Group_Child_Pos_Table";
    public static final String[] ALL_KEYS = new String[] {KEY_ID, KEY_ADD_REMOVE, GROUP_POS, CHILD_POS};

    private static final String POSITION_CREATE_SQL =           
     "CREATE TABLE " + getDatabaseTable() + "("+ 
                                                KEY_ID + " INTEGER PRIMARY KEY," + 
                                                KEY_ADD_REMOVE + " TEXT," + 
                                                GROUP_POS+ " TEXT," + 
                                                CHILD_POS + " TEXT)";   

    private SQLiteDatabase db;
    SQLiteDatabase getWritableDatabase = this.getWritableDatabase();

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CHILD_COUNT_CREATE);
        db.execSQL(POSITION_CREATE_SQL);
    }    

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // on upgrade drop older tables
//        db.execSQL("DROP TABLE IF EXISTS " + CHILD_COUNT_CREATE);
        db.execSQL("DROP TABLE IF EXISTS " + POSITION_CREATE_SQL);
        // create new tables
        onCreate(db);       
    }

MainActivity: 主要活动:

private MySQLite_Database MySQLITE_DATABASE;
private SQLiteDatabase  SQLite_database;
private Data_Holder_Class DATA_HOLDER_CLASS;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_expandable_list_view);             

    //Initialize variables here
    MySQLITE_DATABASE = new MySQLite_Database(this);


try {   

Open_SQLITE_DATABASE();
getAllRecords();
}       
catch (Exception e) 
{
   Log.e("ERROR", "ERROROR");
}


public void getAllRecords() {   
Cursor cursor = SQLite_database.query(MySQLite_Database.DATABASE_TABLE, null, null, null, null, null, null);
if(cursor != null)
    {
    while(cursor.moveToNext()){

        String GROUP_POSITION = cursor.getString(cursor.getColumnIndex("GROUP_POS"));
        String CHILD_POSITION = cursor.getString(cursor.getColumnIndex("CHILD_POS"));

        }
    }       

} }

Thank you very much 非常感谢你

EDIT: posted the working code.. 编辑:发布了工作代码。

I finally found what I did wrong.... 我终于发现自己做错了...。

Here is the complete working code. 这是完整的工作代码。 This may help someone else in the future. 将来可能会对其他人有所帮助。

BAD CODE: 错误代码:

    public void getAllRecords() {   
    Cursor cursor = SQLite_database.query(MySQLite_Database.DATABASE_TABLE, null, null, null, null, null, null);
    if(cursor != null)
        {
        while(cursor.moveToNext()){

            String GROUP_POSITION = cursor.getString(cursor.getColumnIndex("GROUP_POS"));
            String CHILD_POSITION = cursor.getString(cursor.getColumnIndex("CHILD_POS"));

            }
        }       
 }

to

GOOD CODE: 良好代码:

In MySQLite_Database: 在MySQLite_Database中:

I created this method: 我创建了这个方法:

public SQLiteDatabase getWritableData() {
    SQLiteDatabase getWritableDatabase = this.getWritableDatabase();
    return getWritableDatabase;
}

In MainActivity: 在MainActivity中:

MySQLITE_DATABASE = new MySQLite_Database(this);

....

....

public void getAllRecords() {   
Cursor cursor = MySQLITE_DATABASE.getWritableData().query(MySQLite_Database.DATABASE_TABLE, null, null, null, null, null, null);
if(cursor != null)
    {
    while(cursor.moveToNext()){

        String GROUP_POSITION = cursor.getString(cursor.getColumnIndex(MySQLite_Database.GROUP_POS));
        String CHILD_POSITION = cursor.getString(cursor.getColumnIndex(MySQLite_Database.CHILD_POS));

        }
    }       

} }

** Basically, in the BAD CODE, I had **基本上,在错误代码中

private SQLiteDatabase SQLite_database

and used this for the cursor which was wrong. 并将其用于错误的光标。 ** The cursor actually needs this instead **光标实际上需要这个

SQLiteDatabase getWritableDatabase = this.getWritableDatabase(); SQLiteDatabase getWritableDatabase = this.getWritableDatabase();

}

Thanks to everyone for helping, especially mmlooloo. 感谢大家的帮助,尤其是mmlooloo。 mmlooloo really helped me catch another mistakes. mmlooloo确实帮助我发现了另一个错误。 I hope my mistakes helps someone else. 我希望我的错误可以帮助其他人。

change these lines: 更改这些行:

 String GROUP_POSITION = cursor.getString(cursor.getColumnIndex("GROUP_POS"));
 String CHILD_POSITION = cursor.getString(cursor.getColumnIndex("CHILD_POS"));

to

String GROUP_POSITION = cursor.getString(cursor.getColumnIndex(GROUP_POS));
String CHILD_POSITION = cursor.getString(cursor.getColumnIndex(CHILD_POS));

and

Cursor cursor = SQLite_database.query(MySQLite_Database.DATABASE_TABLE, null, null, null, null, null, null);

to

Cursor cursor = MySQLite_Database.query(MySQLite_Database.DATABASE_TABLE, null, null, null, null, null, null);

I think that you don't specify what columns you want int that line 我认为您没有指定要在该行中插入哪些列

Cursor cursor = SQLite_database.query(MySQLite_Database.DATABASE_TABLE, null, null, null, null, null, null); 游标游标= SQLite_database.query(MySQLite_Database.DATABASE_TABLE,null,null,null,null,null,null,null);

the function query needs: 函数查询需要:

String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy 字符串表,字符串[]列,字符串选择,字符串[] selectionArgs,字符串groupBy,具有字符串,字符串orderBy

and in columns you put null, so you have to specify the columns you want 并在列中放置null,因此必须指定所需的列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM