将数据插入Android SQLite数据库时出现空指针异常

时间:2019-05-22 14:31:24

标签: database pointers exception null android-sqlite

我刚刚开始一个Android Studio项目,而我已经创建了一个小型播放器数据库,并且我一直在尝试将播放器详细信息添加到数据库中。但是,每次程序崩溃时都会出现空指针异常。快速调试表明问题出在OnCreate方法中对OnClickListener的调用。需要对代码进行哪些更改?谢谢

create view price_date, current_price as
    select pc.new_price as price
    from price_changes pc
    order by change_date desc
    limit 1;

这是AddPlayer活动类

public class DatabaseHelper {

    // Declare instance of DBHelper class, context and SQLiteDatabase
    private DBHelper ourDbHelper;
    private final Context ourContext;
    private SQLiteDatabase myDb;

    // Initialise the database and table names and version
    private static final String DATABASE_NAME = "Club";
    private static final String PLAYER_TABLE_NAME = "Players";

    //Initialise the PLAYER_TABLE_NAME field names
    private static final String PLAYER_ID = "player_id";
    private static final String PLAYER_SURNAME = "player_surname";
    private static final String PLAYER_FORENAME = "player_forename";
    private static final String PLAYER_AGE = "player_age";
    private static final String PLAYER_DOB = "player_dob";

     private static class DBHelper extends SQLiteOpenHelper {


        // This constructor takes the context, db name and db version as 
    parameters
        // and applies them to the DBHelper class
        public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        // This constructor takes context as parameter and assigns it to 
    variable

        @Override
        public void onCreate(SQLiteDatabase db) {

            //Executes the following queries to create the Player Table
            db.execSQL("CREATE TABLE " + PLAYER_TABLE_NAME + " (" + PLAYER_ID 
    + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + PLAYER_FORENAME + "TEXT NOT NULL, " + PLAYER_SURNAME + 
    "TEXT NOT NULL, " +
                    PLAYER_AGE + " INTEGER NOT NULL, " + PLAYER_DOB + "DATE 
    NOT NULL );");
        }//OnCreate

    @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int 
    newVersion) {

            // if table is upgraded or already exists
            db.execSQL("DROP TABLE IF EXISTS " + PLAYER_TABLE_NAME);

            onCreate(db);
        }//OnUpgrade
    }//DBHelperClass

    // Constructor takes a context as a parameter and assigns it to 
    ourContext
    public DatabaseHelper (Context c){
        ourContext = c;
    }//constructor

    // Open writable database and allow to be modified
    public DatabaseHelper open() throws SQLException {
        ourDbHelper = new DBHelper(ourContext);
        myDb = ourDbHelper.getWritableDatabase();
        return this;
    }//open

    //method to close the db to modification
    public void close() {
        ourDbHelper.close();
    }//close

    // method to create an entry to the Player table with player attributes as parameters
    public long createEntry (String forename, String surname, String age, String dob){

        //contentValues will hold the information to be added to the database
        ContentValues contentValues = new ContentValues();

        //Using put method, insert the parameters into the fields in the table
        contentValues.put(PLAYER_FORENAME, forename);
        contentValues.put(PLAYER_SURNAME, surname);
        contentValues.put(PLAYER_AGE,age);
        contentValues.put(PLAYER_DOB, String.valueOf(dob));

        //insert the values into the player table
        //assign to variable result to check if data added correctly
        return myDb.insert(PLAYER_TABLE_NAME,null,contentValues);

    }//createEntry

1 个答案:

答案 0 :(得分:0)

更改此

DatabaseHelper myDb = new DatabaseHelper(AddPlayer.this);

myDb.open()

收件人

DatabaseHelper myDb = new DatabaseHelper(this.getContext());

myDb.open();
相关问题