【问题标题】:Null pointer Exception while retriving data from database in android从android中的数据库检索数据时出现空指针异常
【发布时间】:2014-11-04 11:39:49
【问题描述】:

大家好,我正在尝试从名为联系人的表中检索数据,但它给了我一个空指针异常

Cursor cursor = database.query(ContactsDBOpenHelper.TABLE_CONTACTS,allcolumns,null,null,null,null,null);

任何人都可以提出什么问题吗? 我在表中有正确的数据。

公共类 ViewAllActivity 扩展 ListActivity {

SQLiteOpenHelper dbhelper;
SQLiteDatabase database;
ContactsDataSource contactsDataSource;

public static final String[] allcolumns = {
        ContactsDBOpenHelper.COLUMN_SEQUENCE,
        ContactsDBOpenHelper.COLUMN_NAME,
        ContactsDBOpenHelper.COLUMN_DEPT,
        ContactsDBOpenHelper.COLUMN_EMAIL,
        ContactsDBOpenHelper.COLUMN_PHONE,
        ContactsDBOpenHelper.COLUMN_ADDRESS,
        ContactsDBOpenHelper.COLUMN_ZIPCODE
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    contactsDataSource = new ContactsDataSource(this);
    contactsDataSource.open();
    dbhelper = new ContactsDBOpenHelper(this);
    dbhelper.getWritableDatabase();

    List<Contact> contacts = viewAllC();
    ArrayAdapter<Contact> adapter = new ArrayAdapter<Contact>(ViewAllActivity.this,R.layout.list,contacts);
    setListAdapter(adapter);

}

public List<Contact> viewAllC() {
    List<Contact> contacts = new ArrayList<Contact>();
    Cursor cursor = database.query(ContactsDBOpenHelper.TABLE_CONTACTS,allcolumns,null,null,null,null,null);
    Log.i("LOGCAT", "rows returned : " + cursor.getCount());
    Contact contact;
    if(cursor.getCount()>0) {
        while(cursor.moveToNext()) {
            contact = new Contact();
            contact.setSequence(cursor.getLong(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_SEQUENCE)));
            contact.setName(cursor.getString(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_NAME)));
            contact.setDepartment(cursor.getString(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_DEPT)));
            contact.setEmail(cursor.getString(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_EMAIL)));
            contact.setPhone(cursor.getString(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_PHONE)));
            contact.setAddress(cursor.getString(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_ADDRESS)));
            contact.setZipCode(cursor.getLong(cursor.getColumnIndex(ContactsDBOpenHelper.COLUMN_ZIPCODE)));
            contacts.add(contact);
        }
    }
    return contacts;

}

}

This is My open helper

public class ContactsDBOpenHelper extends SQLiteOpenHelper {

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

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(TABLE_CREATE);
		Log.i(LOGTAG, "Table has been created");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
		onCreate(db);
	}



}

【问题讨论】:

  • 你能发布你的日志猫吗
  • Log.i("LOGCAT", "返回的行数:" + cursor.getCount());来这里做什么
  • 它不会去那里,因为它上面的行中有一个异常
  • 我忘了初始化什么吗??
  • 可能你忘了设置 cursor.moveToFirst();在if条件之前粘贴它

标签: android database sqlite nullpointerexception


【解决方案1】:

你忘了初始化你的database。

首先实例化您的数据库助手,然后在其上调用 getWritableDatabase() 以获得您可以使用的 SQLiteDatabase:

SQLiteOpenHelper helper = new ContactsDBOpenHelper(this);
database = helper.getWritableDatabase();

【讨论】:

  • 如何初始化数据库和数据库助手??
  • 首先你需要创建一个扩展SQLiteOpenHelper的类。
  • 如何实例化数据库助手??\
  • 你尝试了什么,你在哪里卡住了?请用实际代码更新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-09
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多