在android项目数据库中的空对象引用

时间:2017-09-22 11:31:59

标签: android sqlite

我尝试使用此数据库方法在我的应用中获取所有数据库行:

if (file_exists('path to test.xml')) {
    $xml = simplexml_load_file('path to test.xml');
    echo '<pre>';
    print_r($xml);
} else {
    exit('Failed to open test.xml.');
}

但是当我使用 public class DatabaseHandler extends SQLiteOpenHelper { private static final String ID = "_id"; private static final String unit_id = "unit_id"; private static final String unit_name = "unit_name"; private static final String unit_activity = "activity"; private static final String unit_phone1 = "phone1"; private static final String unit_phone2 = "phone2"; private static final String unit_address = "address"; private static final String unit_number = "number"; private static final String unit_picture_name = "picture_name"; private static final String DATABASE_NAME = "semnanieDB"; private static final String DATABASE_TABLE = "records"; private static final int DATABASE_VERSION = 1; DatabaseHandler(Context context){super(context,DATABASE_NAME,null,DATABASE_VERSION);} @Override public void onCreate(SQLiteDatabase db) { String CREATE_TABLE = "CREATE TABLE " + DATABASE_TABLE + "(" + ID + " INTEGER PRIMARY KEY," + unit_id + " TEXT," + unit_name + " TEXT," + unit_activity + " TEXT," + unit_phone1 + " TEXT," + unit_phone2 + " TEXT," + unit_address + " TEXT," + unit_number + " TEXT," + unit_picture_name + " TEXT)"; db.execSQL(CREATE_TABLE); } public void onUpgrade(SQLiteDatabase db , int oldVersion , int newVersion){ db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); onCreate(db); } List<record> SearchRecord(String word){ List<record> recordList = new ArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); String Select_Query = "SELECT * FROM " + DATABASE_TABLE; Cursor cursor = db.rawQuery(Select_Query,null); if(cursor.moveToFirst()){ do { record record = new record(); record.set_id(Integer.parseInt(cursor.getString(0))); record.set_record_id(cursor.getString(1)); record.set_unit_name(cursor.getString(2)); record.set_activity(cursor.getString(3)); record.set_phone1(cursor.getString(4)); record.set_phone2(cursor.getString(5)); record.set_address(cursor.getString(6)); record.set_unit_number(cursor.getString(7)); record.set_picture(cursor.getString(8)); recordList.add(record); } while (cursor.moveToNext()); } return recordList; } 方法时会出现此错误:

SearchRecord

这是我的记录课

java.lang.RuntimeException: Unable to start activity ComponentInfo{*/*showResultList}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List *.DatabaseHandler.SearchRecord()' on a null object reference

1 个答案:

答案 0 :(得分:0)

您应该在活动中像这样呼叫SearchRecord()

DatabaseHandler databaseHandler = new DatabaseHandler(this);
List<record> records = databaseHandler.SearchRecord();