2017-05-02 8 views
-1

私は奇妙な問題があります。私は自分のアプリケーションでsqliteを使用しています。私は多くのフィールドを持つアクティビティを持っていますが、私はクラスとメソッドを作成しましたが、すべての値を挿入しようとすると「null」と宣言されたのは1つだけで、理由はわかりません。SQLiteは、実際の値を挿入している間に1行にnullを挿入します

Something私がデバッグモードでgetDesignation_coursを実行すると、そのEditTextに入力した内容が取得されていることがわかりますが、ヘルパーではその値の代わりにnullが挿入されます。

私はデータベースに追加私のdatabasehelper

private static final String TABLE_COURS="cours"; 
private static final String COLONNE_IDCOURS="ID"; 
private static final String COLONNE_BRANCHECOURS="branche_cours"; 
private static final String COLONNE_DATEPREMIER="date_debut"; 
private static final String COLONNE_DATEDERNIER="date_dernier"; 
private static final String COLONNE_DEMIPOINT="demi_point"; 
private static final String COLONNE_DIZIEMEPOINT="dizieme_point"; 
private static final String COLONNE_DESIGNATION="designation"; 
private static final String COLONNE_DESCRIPTIONCOURS="description"; 
private static final String COLONNE_LUNDI="lundi"; 
private static final String COLONNE_MARDI="mardi"; 
private static final String COLONNE_MERCREDI="mercredi"; 
private static final String COLONNE_JEUDI="jeudi"; 
private static final String COLONNE_VENDREDI="vendredi"; 
private static final String COLONNE_SAMEDI="samedi"; 

public void insertCours(Cours c) 
{ 
    Open(); 

    ContentValues values = new ContentValues(); 
    values.put(COLONNE_BRANCHECOURS,c.getBranche_cours()); 
    values.put(COLONNE_DATEPREMIER,c.getDate_debut()); 
    values.put(COLONNE_DATEDERNIER,c.getDate_fin()); 
    values.put(COLONNE_DIZIEMEPOINT,c.getDiziemepoint()); 
    values.put(COLONNE_DEMIPOINT,c.getDemipoint()); 
    values.put(COLONNE_DESIGNATION,c.getDesignation_cours()); //this one is the null.. 
    values.put(COLONNE_DESCRIPTIONCOURS,c.getDescription_cours()); 
    values.put(COLONNE_LUNDI,c.getLundi()); 
    values.put(COLONNE_MARDI,c.getMardi()); 
    values.put(COLONNE_MERCREDI,c.getMercredi()); 
    values.put(COLONNE_JEUDI,c.getJeudi()); 
    values.put(COLONNE_VENDREDI,c.getVendredi()); 
    values.put(COLONNE_SAMEDI,c.getSamedi()); 
    db.insert(TABLE_COURS,null,values); 
} 

私の活動

public void AjouterCours(View v) 
{ 
    //récuperation du radioButton qui est selectionne 
    int selectedId = radioGroup.getCheckedRadioButtonId(); 
    radioButton = (RadioButton)findViewById(selectedId); 
    //récupération de la liste deroulante a l'aide de son id 
    spinner = (Spinner)findViewById(R.id.spinner_branches); 

    radioButtonDemi =(RadioButton)findViewById(R.id.RBDemiPoint); 

    radioButtonDizieme =(RadioButton)findViewById(R.id.RBDizieme); 



    //récupération de tous les checkbox 
    cblundi = (CheckBox)findViewById(R.id.CBXLundi); 
    cbmardi = (CheckBox)findViewById(R.id.CBXMardi); 
    cbmercredi = (CheckBox)findViewById(R.id.CBXMercredi); 
    cbjeudi = (CheckBox)findViewById(R.id.CBXJeudi); 
    cbvendredi = (CheckBox)findViewById(R.id.CBXVendredi); 
    cbsamedi = (CheckBox)findViewById(R.id.CBXSamedi); 


    //ce test me permet de savoir si le radioButton pour le demipoint est checké 
    if(radioButtonDemi.isChecked()) { 
     demi =1; 
     dizieme=0; 
    } 
    //s'il l'est pas, cela veut dire que c'est le radioButton pour le diziemepoint de point 
    //qui est checké 
    else { 
     demi=0; 
     dizieme=1; 
    } 

    //avec tous ces tests, j'arrive a determiner si les checkbox 
    //sont cochés ou pas pour après insérer dans la base de données 
    //si c'est choqué, ca vaut 1, sinon ca vaut 0 
    if(cblundi.isChecked()) {lundi =1;} 
    else {lundi = 0;} 

    if(cbmardi.isChecked()) {mardi =1;} 
    else {mardi = 0;} 

    if(cbmercredi.isChecked()) {mercredi =1;} 
    else {mercredi = 0;} 

    if(cbjeudi.isChecked()) {jeudi =1;} 
    else {jeudi = 0;} 

    if(cbvendredi.isChecked()) {vendredi =1;} 
    else {vendredi = 0;} 

    if(cbsamedi.isChecked()) {samedi =1;} 
    else {samedi = 0;} 

//si ca va dans le catch, cela veut dire qu'une branche a été selectionné dans la liste deroulante 
try 
{ 
//j'enregistre toutes les valeurs qui sont insérer dans la 
// base de données par rapport a la branche que j'ai sélectionné 
Cursor cursor = (Cursor) adapter.getItem(spinner.getSelectedItemPosition()); 
//ici, 1 correspond a àa la colonne qui me donne le nom de la branche 
//0 correspondrait au id 
if(cursor!=null) 
{ 

try 
{ 
    String selectedBranche= cursor.getString(1); 
    designation = (EditText)findViewById(R.id.ETDesignationCours); 
    description = (EditText)findViewById(R.id.ETDescriptionCours); 

    String designation_cours = designation.getText().toString(); 
    String description_cours = description.getText().toString(); 

    dbhelper.Open(); 

    Cours c = new Cours(); 
    c.setBranche_cours(selectedBranche); 
    c.setDate_debut(datedebut.getText().toString()); 
    c.setDate_fin(datefin.getText().toString()); 
    c.setDesignation_cours(designation_cours); 
    c.setDescription_cours(description_cours); 
    c.setDemipoint(demi); 
    c.setDiziemepoint(dizieme); 
    c.setLundi(lundi); 
    c.setMardi(mardi); 
    c.setMercredi(mercredi); 
    c.setJeudi(jeudi); 
    c.setVendredi(vendredi); 
    c.setSamedi(samedi); 


    dbhelper.insertCours(c); 

    Intent retour = new Intent(AjoutCours.this,MesCours.class); 
    startActivity(retour); 
}catch(Exception e) 
{ 
    Toast.makeText(AjoutCours.this, 
      "error",Toast.LENGTH_SHORT).show(); 
} 
UPDATE-LOGCAT @

05-02 06:17:12.048 25155-25155/com.example.dasilvadd.students E/SQLiteDatabase: Error inserting vendredi=1 lundi=0 description=dsadas designation=null demi_point=1 date_debut=18.5.2017 jeudi=0 branche_cours=bonjour samedi=1 dizieme_point=0 mercredi=0 date_dernier=7.6.2017 mardi=0 
                      android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: cours.designation (code 1299) 
                       at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method) 
                       at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782) 
                       at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788) 
                       at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86) 
                       at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1471) 
                       at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341) 
                       at com.example.dasilvadd.students.DatabaseHelper.insertCours(DatabaseHelper.java:168) 
                       at com.example.dasilvadd.students.AjoutCours.AjouterCours(AjoutCours.java:414) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at java.lang.reflect.Method.invoke(Method.java:372) 
                       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                       at android.view.View.performClick(View.java:4780) 
                       at android.view.View$PerformClick.run(View.java:19866) 
                       at android.os.Handler.handleCallback(Handler.java:739) 
                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                       at android.os.Looper.loop(Looper.java:135) 
                       at android.app.ActivityThread.main(ActivityThread.java:5254) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at java.lang.reflect.Method.invoke(Method.java:372) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

君たちをありがとう!

+0

この投稿に「nullpointerexception」というタグが付いているのはなぜですか? –

+0

私は実際のものの代わりにヌル値を得ているので、私はこの投稿にタグを付けると間違っていますか? – David

+0

あなたはNullPointerExceptionが何であるか知っていますか?インスタンス化されずに使用されたオブジェクトのため、アプリケーションにクラッシュしています。レコードセットにnull値を持たせることで** **何もしません。 –

答えて

0

答え:私のクラスCours(get-settersを使用)で、私は削除して、その列の取得と設定を書き直しました。

関連する問題