Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to create a table Factory and to insert data in this table. But i don't know why it give me this error in logcat :

07-22 10:49:48.480 2471-2471/com.example.pc.myapplication E/SQLiteDatabase: Error inserting U4=a U1=aaaaaaa A4=lll Q5=aaaaaaaaaaaaaaa Q4=null A3=aaaaaaaaaaa A5=llll P5=a P3=a P4=a U2=a Q1=aaaaaaaaaa P2=aa A1=llll Q2=aaaaaaaaa U5=a Fournisseur=kkkk Q3=aaaaaaaaaaa U3=a Datefactu=10022001 Numero=123 A2=kkkk Mode=null P1=a
    android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: Facture.Mode (code 1299)
        at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
        
        at android.os.Handler.dispatchMessage(Handler.java:95)


And there is my class facture1.java :

package com.example.pc.myapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class facture1 extends Activity {

    DatabaseHelper helper = new DatabaseHelper(this);

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

        }

    public void onOKClick ( View v )
    {

        if (v.getId() == R.id.buttonfacok) {

            EditText numero = (EditText)findViewById(R.id.Etnum);
            EditText date = (EditText)findViewById(R.id.Etdat);
           EditText fournisseur = (EditText)findViewById(R.id.TVfour);
            EditText modepaiement = (EditText)findViewById(R.id.TVmode);
            EditText article1 = (EditText)findViewById(R.id.A1);
            EditText article2 = (EditText)findViewById(R.id.A2);
            EditText article3 = (EditText)findViewById(R.id.A3);
            EditText article4 = (EditText)findViewById(R.id.A4);
            EditText article5 = (EditText)findViewById(R.id.A5);
            EditText quantite1 = (EditText)findViewById(R.id.Q1);
            EditText quantite2 = (EditText)findViewById(R.id.Q2);
            EditText quantite3 = (EditText)findViewById(R.id.Q3);
            EditText quantite4 = (EditText)findViewById(R.id.Q4);
            EditText quantite5 = (EditText)findViewById(R.id.Q5);
            EditText unite1 = (EditText)findViewById(R.id.U1);
            EditText unite2 = (EditText)findViewById(R.id.U2);
            EditText unite3 = (EditText)findViewById(R.id.U3);
            EditText unite4 = (EditText)findViewById(R.id.U4);
            EditText unite5 = (EditText)findViewById(R.id.U5);
            EditText prix1 = (EditText)findViewById(R.id.P1);
            EditText prix2 = (EditText)findViewById(R.id.P2);
            EditText prix3 = (EditText)findViewById(R.id.P3);
            EditText prix4 = (EditText)findViewById(R.id.P4);
            EditText prix5 = (EditText)findViewById(R.id.P5);


            String strnumero = numero.getText().toString();
            String strdate = date.getText().toString();
            String strfournisseur = fournisseur.getText().toString();
            String strmodepaiement = modepaiement.getText().toString();
            String strarticle1 = article1.getText().toString();
            String strarticle2 = article2.getText().toString();
            String strarticle3 = article3.getText().toString();
            String strarticle4 = article4.getText().toString();
            String strarticle5 = article5.getText().toString();
            String strquantite1 = quantite1.getText().toString();
            String strquantite2 = quantite2.getText().toString();
            String strquantite3 = quantite3.getText().toString();
            String strquantite4 = quantite4.getText().toString();
            String strquantite5 = quantite5.getText().toString();
            String strunite1 = unite1.getText().toString();
            String strunite2 = unite2.getText().toString();
            String strunite3 = unite3.getText().toString();
            String strunite4 = unite4.getText().toString();
            String strunite5 = unite5.getText().toString();
            String strprix1 = prix1.getText().toString();
            String strprix2 = prix2.getText().toString();
            String strprix3 = prix3.getText().toString();
            String strprix4 = prix4.getText().toString();
            String strprix5 = prix5.getText().toString();


            Facture f = new Facture();
            f.setNumero(strnumero);
            f.setDatefactu(strdate);
            f.setFournisseur(strfournisseur);
            f.setMode(strmodepaiement);
            f.setA1(strarticle1);
            f.setA2(strarticle2);
            f.setA3(strarticle3);
            f.setA4(strarticle4);
            f.setA5(strarticle5);
            f.setQ1(strquantite1);
            f.setQ2(strquantite2);
            f.setQ3(strquantite3);
            f.setQ4(strquantite4);
            f.setQ5(strquantite5);
            f.setU1(strunite1);
            f.setU2(strunite2);
            f.setU3(strunite3);
            f.setU4(strunite4);
            f.setU5(strunite5);
            f.setP1(strprix1);
            f.setP2(strprix2);
            f.setP3(strprix3);
            f.setP4(strprix4);
            f.setP5(strprix5);

            helper.insertFacture(f);

}
    }
}

Thanks in advance

What I have tried:

There is my databasehelper.java :

package com.example.pc.myapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 4 ;
    private static final String DATABASE_NAME = "contacts.db" ;
    private static final String TABlE_NAME = "contacts" ;
    private static final String COLUMN_ID = "id" ;
    private static final String COLUMN_NOMETPRENOM = "nometprenom" ;
    private static final String COLUMN_CIN = "cin" ;
    private static final String COLUMN_MOTDEPASSE = "motdepasse" ;
    private static final String TABlE_NAME1 = "Argent" ;
    private static final String COLUMN_ENTREE = "Entree" ;
    private static final String COLUMN_DATE = "date" ;
    private static final String TABlE_NAME2 = "Facture" ;
    private static final String COLUMN_NUMERO = "Numero" ;
    private static final String COLUMN_DATEFOU = "Datefactu" ;
    private static final String COLUMN_FOURNISSEUR = "Fournisseur" ;
    private static final String COLUMN_MODE = "Mode" ;
    private static final String COLUMN_Article1 = "A1" ;
    private static final String COLUMN_Article2 = "A2" ;
    private static final String COLUMN_Article3 = "A3" ;
    private static final String COLUMN_Article4 = "A4" ;
    private static final String COLUMN_Article5 = "A5" ;
    private static final String COLUMN_Quantite1 = "Q1" ;
    private static final String COLUMN_Quantite2 = "Q2" ;
    private static final String COLUMN_Quantite3 = "Q3" ;
    private static final String COLUMN_Quantite4 = "Q4" ;
    private static final String COLUMN_Quantite5= "Q5" ;
    private static final String COLUMN_Unitaire1 = "U1" ;
    private static final String COLUMN_Unitaire2 = "U2" ;
    private static final String COLUMN_Unitaire3 = "U3" ;
    private static final String COLUMN_Unitaire4 = "U4" ;
    private static final String COLUMN_Unitaire5 = "U5" ;
    private static final String COLUMN_Prix1 = "P1" ;
    private static final String COLUMN_Prix2 = "P2" ;
    private static final String COLUMN_Prix3 = "P3" ;
    private static final String COLUMN_Prix4 = "P4" ;
    private static final String COLUMN_Prix5 = "P5" ;

    SQLiteDatabase db;


    private static final String TABlE_CREATE = "create table contacts ( id integer primary key not null , nometprenom Text not null , cin Text not null , motdepasse Text not null);" ;
    private static final String TABlE_CREATE1 = "create table Argent ( id integer primary key not null , date Text not null , Entree Text not null);" ;
    private static final String TABlE_CREATE2 = "create table Facture ( id integer primary key not null , Datefactu Text not null , Numero Text not null , Fournisseur Text not null , Mode Text not null , A1 Text not null , A2 Text not null , A3 Text not null , A4 Text not null , A5 Text not null , Q1 Text not null , Q2 Text not null , Q3 Text not null , Q4 Text not null , Q5 Text not null , U1 Text not null , U2 Text not null , U3 Text not null , U4 Text not null , U5 Text not null , P1 Text not null , P2 Text not null , P3 Text not null , P4 Text not null , P5 Text not null);" ;

    public DatabaseHelper(Context context)
    {
        super(context ,DATABASE_NAME , null , DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABlE_CREATE2);
        this.db=db;
    }

    
    public void insertFacture(Facture f)
    {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        String query = "select * from Facture" ;
        Cursor cursor = db.rawQuery(query , null) ;
        int count = cursor.getCount();

        values.put(COLUMN_NUMERO, f.getNumero());
        values.put(COLUMN_DATEFOU , f.getDatefactu());
        values.put(COLUMN_FOURNISSEUR , f.getFournisseur());
        values.put(COLUMN_MODE, f.getMode());
        values.put(COLUMN_Article1, f.getA1());
        values.put(COLUMN_Article2, f.getA2());
        values.put(COLUMN_Article3, f.getA3());
        values.put(COLUMN_Article4, f.getA4());
        values.put(COLUMN_Article5, f.getA5());
        values.put(COLUMN_Quantite1, f.getQ1());
        values.put(COLUMN_Quantite2, f.getQ2());
        values.put(COLUMN_Quantite3, f.getQ3());
        values.put(COLUMN_Quantite4, f.getQ4());
        values.put(COLUMN_Quantite5, f.getQ5());
        values.put(COLUMN_Unitaire1, f.getU1());
        values.put(COLUMN_Unitaire2, f.getU2());
        values.put(COLUMN_Unitaire3, f.getU3());
        values.put(COLUMN_Unitaire4, f.getU4());
        values.put(COLUMN_Unitaire5, f.getU5());
        values.put(COLUMN_Prix1, f.getP1());
        values.put(COLUMN_Prix2, f.getP2());
        values.put(COLUMN_Prix3, f.getP3());
        values.put(COLUMN_Prix4, f.getP4());
        values.put(COLUMN_Prix5, f.getP5());

        db.insert(TABlE_NAME2,null, values);
    }




    @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
               
              String query2 = "DROP TABLE IF EXISTS "+TABlE_NAME2 ;
              
                db.execSQL(query2);
                this.onCreate(db);
            }

            }
Posted
Updated 22-Jul-18 1:40am

SQLiteConstraintException: NOT NULL constraint failed:

The message is clear, you have null items where your table specifies that a column entry cannot be null. Look at your insert statement and compare with your table description.
 
Share this answer
 
Look at the error message, it's pretty explicit:
NOT NULL constraint failed: Facture.Mode
It means that when you tried to insert the row, the value you passed to the DB was NULL, and you specifically said it can;t be that when you create the table.
So look at where you set and get the value: getMode and the line
f.setMode(strmodepaiement);
And find out exactly what you are passing, and why.

Sorry, but we can't do that for you - we don't have any access to your DB or your data!
 
Share this answer
 
Comments
Member 13901696 22-Jul-18 10:43am    
thanks. The structure of database is mentionned in the database helper. Mode is a parameter declared as not null but it take always null. Any help please ?
OriginalGriff 22-Jul-18 10:57am    
What part of "Sorry, but we can't do that for you - we don't have any access to your DB or your data!" is difficult to understand?

We literally cannot do that for you - we can't run your code under the same circumstances you can. So grab a debugger, and start looking at where the NULL is coming from, because you are the only person who can ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900