jeudi 26 novembre 2015

android.database.sqlite.SQLiteConstraintException: *string* may not be NULL

I have a datepicker where user select a date then the selected date will be converted to string (String sel_date) and displayed under the date picker. The string does displayed fine but when I tried to save the string into database, I get this error

E/SQLiteDatabase: Error inserting 
E/SQLiteDatabase: android.database.sqlite.SQLiteConstraintException: xpirer2.expiry_date may not be NULL (code 19)

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

public static final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
Calendar calendar = Calendar.getInstance();
String sel_date;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){

    //use current date as default date on date picker
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int date = calendar.get(Calendar.DATE);

    //create new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, date);
}

@Override
public void onDateSet(DatePicker view, int year, int month, int date) {

    calendar.set(year, month + 1, date);
    String mon = months[month];

    //get the selected date
    sel_date = ""+view.getDayOfMonth()+" "+(mon)+" " +view.getYear();

    //Display the selected date after the date has been set
    TextView dateText = (TextView)getActivity().findViewById(R.id.date_selected);
    dateText.setText(sel_date);//the string works fine here
}

Since I get the error when trying to save, so I'll just paste that part of my code.

MyFragment.java

@Override
public boolean onOptionsItemSelected(MenuItem xpirer){
    switch (xpirer.getItemId()){
        case R.id.xpirer_delete:
            //delete 
            if(currentXpirer != null){
                promptForDelete();
            }
            else{
                makeToast("Cannot delete item that has not been saved");
            }
            break;
        case R.id.xpirer_save:
            //save 
            if(saveXpirer()){
                makeToast(currentXpirer != null ? "Item updated" : "Item saved");
            }
            break;
    }
    return super.onOptionsItemSelected(xpirer);
}


private void makeToast(String message){
    Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
}

private boolean saveXpirer(){

    Note = noteEditText.getText().toString();

    String item = itemEditText.getText().toString();
    if(TextUtils.isEmpty(item)){
        itemEditText.setError("Item is required");
        return false;
    }

    String quantity = quantityEditText.getText().toString();
    if(TextUtils.isEmpty(quantity)){
        quantityEditText.setError("Quantity is required");
        return false;
    }

    if(selUnit.equals("None")){
        unitError = (TextView)spinner.getSelectedView();
        unitError.setError("");
        unitError.setTextColor(Color.RED);
        unitError.setTextSize(R.dimen.text_size_normal);
        unitError.setText("Select a unit!");
        return false;
    }

    if(currentXpirer != null){
        currentXpirer.setItem(item);
        currentXpirer.setQuantity(quantity);
        currentXpirer.setSelUnit(selUnit);
        currentXpirer.setSel_date(sel_date);
        XpirerManager.newInstance(getActivity()).update(currentXpirer);
    }

    else {
        Xpirer xpirer = new Xpirer();
        xpirer.setItem(item);
        xpirer.setQuantity(quantity);
        xpirer.setSelUnit(selUnit);
        xpirer.setSel_date(sel_date);
        xpirer.setNote(Note);
        XpirerManager.newInstance(getActivity()).create(xpirer);
    }
    return true;
}

private void populateFields(){
    itemEditText.setText(currentXpirer.getItem());
    quantityEditText.setText(currentXpirer.getQuantity());
    noteEditText.setText(currentXpirer.getNote());
    }

DbHelper.java

public class DbHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "Xpirer.db";
private static final int DATABASE_VERSION = 1;

//create pantry table statement
private static final String CREATE_TABLE_XPIRER = "create table "
        + Constants.XPIRER_TABLE
        + "("
        + Constants.COLUMN_ID + " integer primary key AUTOINCREMENT, "
        + Constants.COLUMN_ITEM + " text not null, "
        + Constants.COLUMN_QUANTITY + " integer not null, "
        + Constants.COLUMN_UNIT + " text not null, "
        + Constants.COLUMN_EXPIRY_DATE + " text not null, "
        + Constants.COLUMN_NOTE + " text not null "
        + ")";

public DbHelper(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {

    //create the required table
    db.execSQL(CREATE_TABLE_XPIRER);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + Constants.XPIRER_TABLE);
    //create new table
    onCreate(db);
}

}

Aucun commentaire:

Enregistrer un commentaire