SQLite数据库错误空指针



试图从SQLitedatabase获取数据,但出现以下错误:

android.database.CursorIndexOutOfBoundExceptions: Index -1 requested with a size of 4

请告诉我这个错误正在发生。提前感谢

这是我所做的全部代码:

DataBaseHelper.java

public class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context context, String name,CursorFactory factory, int version) 
{
           super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase _db) 
{
        _db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) 
{
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to "
+_newVersion + ", which will destroy all old data");
_db.execSQL("DROP TABLE IF EXISTS " + "LOGIN2");
    onCreate(_db);
}
}

登录数据库帮助程序.java

public class LoginDataBaseAdapter 

ArrayList<String> results=new ArrayList<String>();
    static final String DATABASE_NAME = "login2.db";
    static final int DATABASE_VERSION = 1;
    public static final int NAME_COLUMN = 1;
    public static final String Key_id="_id";
static final String DATABASE_CREATE = "create table "+"LOGIN2"+
"( " +"Key_id"+" integer primary key autoincrement,"
+ "NAME  text, FATHERNAME text, MOTHERNAME text,
DOB integer, ADDRESS text, PHONE integer, GENDER text,
MARITALSTATUS text, EMAIL text, USERNAME text,
PASSWORD text); ";
public  SQLiteDatabase db;
private final Context context;
private DataBaseHelper dbHelper;
    public  LoginDataBaseAdapter(Context _context) 
    {
        context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
public  LoginDataBaseAdapter open() throws SQLException 
    {
        db = dbHelper.getWritableDatabase();
        return this;
    }
    public void close() 
    {
        db.close();
    }
    public  SQLiteDatabase getDatabaseInstance()
    {
        return db;
    }
public void insertEntry(String name,String father,
String mother, String dob, String address, String phone,
String gender,String marital,String email,String username,
String password )
{
       ContentValues newValues = new ContentValues();
newValues.put("NAME", name);
        newValues.put("FATHERNAME",father);
        newValues.put("MOTHERNAME", mother);
        newValues.put("DOB", dob);
        newValues.put("ADDRESS", address);
        newValues.put("PHONE", phone);
        newValues.put("GENDER", gender);
        newValues.put("MARITALSTATUS", marital);
        newValues.put("EMAIL", email);
        newValues.put("USERNAME", username);
        newValues.put("PASSWORD", password);
db.insert("LOGIN2", null, newValues);
}
    public int deleteEntry(String Address)
    {
String where="ADDRESS=?";
        int numberOFEntriesDeleted=
db.delete("LOGIN2", where, new String[]{Address}) ;
return numberOFEntriesDeleted;
    }   
    public String getSinlgeEntry(String userName)
    {
Cursor cursor=db.query("LOGIN2", null, " USERNAME=?",
new String[]{userName}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
        {
            cursor.close();
            return "NOT EXIST";
        }
        cursor.moveToFirst();
String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
        cursor.close();
        return password;                
    }
public void  updateEntry(String userName,String password, String roll)
    {
ContentValues updatedValues = new ContentValues();
updatedValues.put("USERNAME", userName);
        updatedValues.put("PASSWORD",password);
        updatedValues.put("ROLL", roll);
        String where="USERNAME = ?";
db.update("LOGIN2",updatedValues, where, new String[]{userName});              
    }
    public ArrayList<String> getFriends()
    {
ArrayList<String> result=new ArrayList<String>();
Cursor c=db.rawQuery("SELECT * from"+" LOGIN2"+" ", null);
        do
        {
            String strr=c.getString(c.getColumnIndex("USERNAME"));
            result.add(strr);
        }while(c.moveToNext());
        return result;
}}

我想在列表视图中根据用户名字段打印所有用户。这是我为它写的代码。

List.java

public class List extends ListActivity
{

ListView list1;
LoginDataBaseAdapter loginDataBaseAdapter;
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
    loginDataBaseAdapter=loginDataBaseAdapter.open();
    ArrayList<String> from=loginDataBaseAdapter.getFriends();
ArrayAdapter<String> adapter=new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,from);
list1.setAdapter(adapter);
    }}

正在获取CursorIndexOutOfBoundException。

如果您想遍历所有行,这将对您有所帮助:

Cursor c = db.query(TABLENAME, null, null, null, null, null, null); 
while(c.moveToNext()) {
      int id = c.getInt(c.getColumnIndex("_ID")); 
}

或者您可以使用其他光标函数,例如moveToPosition()来访问id 指定的行

更多信息:http://developer.android.com/reference/android/database/Cursor.html

    if(cursor.getCount()>0){  
   Cursor cursor = db.rawQuery(selectQuery, null);

      if (cursor.moveToFirst()) {
          do {
              String data=cursor.getString(columnindex);

          } while (cursor.moveToNext());
      }
       }
      cursor.close();
      db.close();

尝试此代码并检查第一个cursor.getCount()以查看值是否在cursor中。

第一次调用返回boolean valuecursor.movetoFirst();方法

if(cursor.moveToFirst()){
do{
  // cursor.getString(0);.......
  }while(cursor.moveToNext());}

最新更新