插入数据库时​​出现NullPointerException

时间:2015-04-02 02:46:46

标签: java android sql database nullpointerexception

我正在使用数据库开发程序。 我坚持插入从EditText键入的数据,继续获取NullPointerException。 这是我的代码:

AddBusDataActivity.java

public class AddBusDataActivity extends ActionBarActivity {
    SQLiteDatabase mBusDatabse;
    BusDatabaseHelper mBusDatabaseHelper;
    EditText mNumber;
    EditText mDestination;
    EditText mArrivalTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        mBusDatabaseHelper = new BusDatabaseHelper(AddBusDataActivity.this, Constants.DATABASE_NAME, null, Constants.VERSION);

        mNumber = (EditText)findViewById(R.id.number_text);
        mDestination = (EditText)findViewById(R.id.destination_text);
        mArrivalTime = (EditText)findViewById(R.id.arrival_time);
    }
    public void addBusActivityButton(View view) {
        mBusDatabse = mBusDatabaseHelper.getWritableDatabase();
        BusData bus = new BusData(mNumber.getText().toString(), mDestination.getText().toString(), Integer.valueOf(mArrivalTime.getText().toString()));
        mBusDatabaseHelper.addBusData(bus); // error here
        mBusDatabaseHelper.close();
        Toast.makeText(AddBusDataActivity.this, "Bus data successfully added", Toast.LENGTH_SHORT).show();
        this.finish();
    }
}

BusDatabaseHelper.java

public class BusDatabaseHelper extends SQLiteOpenHelper {
    SQLiteDatabase mBusDatabase;

    public BusDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table "+ Constants.TABLE_NAME + " (" + Constants.KEY_ID + " integer primary key autoincrement, " +
                Constants.NUMBER + " text not null, " + Constants.DESTINATION + " text not null, " + Constants.ARRIVAL_TIME + " integer not null);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table " + Constants.TABLE_NAME + ";");
        onCreate(db);
    }

    public void addBusData(BusData busData) {
        ContentValues busValues = new ContentValues();
        busValues.put(Constants.NUMBER, busData.getmNumber());
        busValues.put(Constants.DESTINATION, busData.getmDestination());
        busValues.put(Constants.ARRIVAL_TIME, busData.getmArrivalTime());
        mBusDatabase.insert(Constants.TABLE_NAME, null, busValues); // error here
    }
}

错误消息

 Caused by: java.lang.NullPointerException
            at com.id11201478.exercise6.BusDatabaseHelper.addBusData(BusDatabaseHelper.java:36)
            at com.id11201478.exercise6.AddBusDataActivity.addBusActivityButton(AddBusDataActivity.java:36)

我有单独的 Constants.java 类来存储thoses值。

提前致谢!

2 个答案:

答案 0 :(得分:1)

mBusDatabase中的BusDatabaseHelper似乎从未初始化,因此为空。在第36行取消引用它会导致您的NPE。

如果您每次修改addBusData以获取它,则实际上并不需要它:

public void addBusData(BusData busData) {
    ContentValues busValues = new ContentValues();
    busValues.put(Constants.NUMBER, busData.getmNumber());
    busValues.put(Constants.DESTINATION, busData.getmDestination());
    busValues.put(Constants.ARRIVAL_TIME, busData.getmArrivalTime());
    getWritableDatabase().insert(Constants.TABLE_NAME, null, busValues);
}

答案 1 :(得分:1)

变量

BusDatabaseHelper.mBusDatabase

为空。

你需要初始化它!

否则系统如何知道mBusDatabase是什么

尝试在插入

之前添加它
mBusDatabase= this.getWritableDatabase();