Android java.lang.NullPointerException:尝试调用虚方法'android.database.Cursor

时间:2018-03-25 16:36:11

标签: java android sqlite

我在尝试添加属性ID,名称,电子邮件,电话号码和微调数据(来自所有者)时遇到此错误。这是我的数据库......

public Owner getOwnerById(long id) {
    Cursor cursor = sqLiteDatabase.query(DataBaseHelper.TABLE_NAME, mAllKey,
            DataBaseHelper.KEY_ID + " = ?",
            new String[] { String.valueOf(id) }, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }

    Owner owner = cursorToOwner(cursor);
    return owner;
}

public Property addProperty (int id,String name, String email, String 
phoneNumber, long ownerId) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(DataBaseHelper.PROPERTY_ID, id);
    values.put(DataBaseHelper.PROPERTY_NAME, name);
    values.put(DataBaseHelper.PROPERTY_EMAIL, email);
    values.put(DataBaseHelper.PROPERTY_PHONENUMBER, phoneNumber);
    values.put(DataBaseHelper.PROPERTY_OWNER_ID, ownerId);
    long insertId = db
            .insert(DataBaseHelper.TABLE_PROPERTY, null, values);
    Cursor cursor = db.query(DataBaseHelper.TABLE_PROPERTY, mAllColumns,
            DataBaseHelper.PROPERTY_ID + " = " + insertId, null, null,
            null, null);
    cursor.moveToFirst();

    Property newProperty = cursorToProperty(cursor);
    cursor.close();
    return newProperty;
  }

  private Property cursorToProperty(Cursor cursor) {
    Property property = new Property();
    property.setId(cursor.getInt(0));
    property.setName(cursor.getString(1));
    property.setEmail(cursor.getString(2));
    property.setPhoneNumber(cursor.getString(3));

    // get The company by id
    long ownerId = cursor.getLong(4);

    DataBaseHelper db = new DataBaseHelper(mContext);
    Owner owner = db.getOwnerById(ownerId);
    if (owner != null)
        property.setOwner(owner);

    return property;
   }

logcat的

  java.lang.NullPointerException: Attempt to invoke virtual method 
 'android.database.Cursor 
android.database.sqlite.SQLiteDatabase.query(java.lang.String, j 
java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, 
java.lang.String, java.lang.String)' on a null object reference at
getOwnerById(DataBaseHelper.java:165)
cursorToProperty(DataBaseHelper.java:307)
addProperty(DataBaseHelper.java:223)
onClick(PropertyActivity.java:112)

我的想法是添加一个所有者详细信息,然后它将传递给Property activity spinner。之后,该微调器数据(所有者)和属性数据将添加到数据库中。

0 个答案:

没有答案