jeudi 29 octobre 2015

Inserting data in SQLite database [duplicate]

This question already has an answer here:

I'm trying to insert some data in my database, but I have some Errors.
I searched for a solution in old questions, but I didn't find the solution.

Can someone help me?

I can't insert any data in my database.

Here is the Error:

Caused by: java.lang.NullPointerException
at com.example.yassin.projetquizz.BasedeDonee.insertData(BasedeDonee.java:92)
at com.example.yassin.projetquizz.AddQuizz.buttonQuestionNextListener(AddQuizz.java:134)

Here is my code:

SqliteOpenhelper

public class BasedeDonee extends SQLiteOpenHelper {

public static final String Table_Name="Example";
public String DATABASE_CREATE1 = "CREATE TABLE " + Table_Name
        + " (_id integer primary key autoincrement, "
        + "quizz TEXT,"
        + "Question TEXT,"
        + "Reponse1 TEXT,"
        + "Reponse2 TEXT,"
        + "Reponse3 TEXT,"
        + "Reponse4 TEXT,"
        + "Reponse5 TEXT,"
        + "Reponse6 TEXT,"
        + "Reponse7 TEXT,"
        + "TrueReponse TEXT);";
SQLiteDatabase db;
public static final int DATABASE_VERSION = 1;
int j = 0;




public BasedeDonee(Context context) {
    super(context, TableData.TableInfo.DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase database) {

    this.db = database;
    db.execSQL(DATABASE_CREATE1);
    db.execSQL("INSERT INTO " + Table_Name + " (Question, quizz, Reponse1, Reponse2, TrueReponse) VALUES ('First Question','QUIZZ 1','Reponse 1','Reponse 2','Reponse 1')");
    db.execSQL("INSERT INTO " + Table_Name + " (Question, quizz, Reponse1, Reponse2, TrueReponse) VALUES ('Second Question','QUIZZ 1','Reponse 1','Reponse 2','Reponse 2')");
    db.execSQL("INSERT INTO " + Table_Name + " (Question, quizz, Reponse1, Reponse2, TrueReponse) VALUES ('Third Question','QUIZZ 2','Reponse 1', 'Answer 2', 'Reponse 1')");
    db.execSQL("INSERT INTO " + Table_Name + " (Question, quizz, Reponse1, Reponse2, TrueReponse) VALUES ('Fourth Question','QUIZZ 3','Reponse 1', 'Answer 2', 'Answer 2')");

}


@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        /* Pas pour le moment */
}

public Cursor getTableName() {
    this.db = this.getWritableDatabase();
    return db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' And name is not 'sqlite_sequence' And name is not'android_metadata'", null);
    // return db.rawQuery("SELECT quizz FROM "+Table_Name, null);

}

public Cursor getTableContent() {
    this.db = this.getWritableDatabase();
    return db.rawQuery("SELECT Question FROM " + Table_Name, null);
}

public Cursor getTableContents() {
    this.db = this.getWritableDatabase();
    return db.rawQuery("SELECT * FROM " + Table_Name, null);
}


public void insertData(String question, String[] table, String reponsevrai, String quizz) {

    ContentValues cv = new ContentValues();
    cv.put("Question", question);
    cv.put("quizz", quizz);
    cv.put("Reponse1", table[0]);
    cv.put("Reponse2", table[1]);
    cv.put("Reponse3", table[2]);
    cv.put("Reponse4", table[3]);
    cv.put("Reponse5", table[4]);
    cv.put("Reponse6", table[5]);
    cv.put("Reponse7", table[6]);

    cv.put("TrueReponse", reponsevrai);
    db.insert(Table_Name, null, cv);
    Log.d("AddQuizz", "Insertion of Data succed");
}

public void chargerLesQuizzs(List<String> lcs) {

    Cursor c = this.getTableName();

    if (c.moveToFirst()) {
        while (!c.isAfterLast()) {
            lcs.add(c.getString(c.getColumnIndex("name")));
            c.moveToNext();
        }
    }
    c.close();
}

public void chargerLesQuestions(List<String> lcs) {
    Cursor cursor = this.getTableContent();
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        String Question = cursor.getString(0);
        lcs.add(Question);
        cursor.moveToNext();
    }
    cursor.close();
}

public void chargerLesDonees(Question[] q) {
    String[] quest = new String[7];
    Cursor cursor = this.getTableContents();
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        quest[0] = cursor.getString(cursor.getColumnIndex("Reponse1"));
        quest[1] = cursor.getString(cursor.getColumnIndex("Reponse2"));
        quest[2] = cursor.getString(cursor.getColumnIndex("Reponse3"));
        quest[3] = cursor.getString(cursor.getColumnIndex("Reponse4"));
        quest[4] = cursor.getString(cursor.getColumnIndex("Reponse5"));
        quest[5] = cursor.getString(cursor.getColumnIndex("Reponse6"));
        quest[6] = cursor.getString(cursor.getColumnIndex("Reponse7"));

        q[j] = new Question(cursor.getString(cursor.getColumnIndex("Question")), quest, cursor.getString(cursor.getColumnIndex("TrueReponse")),cursor.getString(cursor.getColumnIndex("quizz")));
        j++;

        cursor.moveToNext();
    }
    cursor.close();
}
}

And here is an activity:

public class AddQuizz extends Activity {

Question q[]=new Question[100];
String [] T=new String[7];
int j=0;//itérateur pour l'objet Question.
int i=0;//itérateur pour l'objet reponse.

EditText Questions, Reponses, Name;
Button SaveQuestion, Instruction, NextQuestion, SaveReponse, SaveQuizz;
RadioGroup radios;
RadioButton vrai, faux;

ListView ListSaisi;
List<String> listQuestion = new ArrayList<String>();
ArrayAdapter<String> adapter;

BasedeDonee db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_quizz);
    Questions=(EditText)findViewById(R.id.editTextQuestion);
    Reponses=(EditText)findViewById(R.id.editTextReponse);
    Name=(EditText)findViewById(R.id.SaveName);

    SaveQuestion=(Button)findViewById(R.id.SaveQuestion);
    SaveReponse=(Button)findViewById(R.id.SaveReponse);
    Instruction=(Button)findViewById(R.id.buttonInstruction);
    NextQuestion=(Button)findViewById(R.id.buttonQuestionNext);
    SaveQuizz=(Button)findViewById(R.id.buttonSaveName);

    vrai=(RadioButton)findViewById(R.id.VraiRadioButton);
    faux=(RadioButton)findViewById(R.id.FauxRadioButton);
    radios=(RadioGroup)findViewById(R.id.group);

    ListSaisi=(ListView) findViewById(R.id.listViewReponses);

    SaveQuestion.setVisibility(View.INVISIBLE);
    SaveReponse.setVisibility(View.INVISIBLE);
    SaveQuestion.setVisibility(View.INVISIBLE);
    NextQuestion.setVisibility(View.INVISIBLE);
    Questions.setVisibility(View.INVISIBLE);
    Reponses.setVisibility(View.INVISIBLE);
    radios.setVisibility(View.INVISIBLE);
    ListSaisi.setVisibility(View.INVISIBLE);

    adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listQuestion);
    ListSaisi.setAdapter(adapter);

    db=new BasedeDonee(this);
    q[j]=new Question("Q",T,"VR","N");
}

public void saveNameListener(View v){

    q[j].NomQuizz=Name.getText().toString();


    Questions.setVisibility(View.VISIBLE);
    SaveQuestion.setVisibility(View.VISIBLE);

    Name.setVisibility(View.INVISIBLE);
    SaveQuizz.setVisibility(View.INVISIBLE);
    Log.d("AddQuizz", "Nom du Quizz est " + q[j].NomQuizz);
    Toast.makeText(this,"Nom du Quizz est "+ q[j].NomQuizz,Toast.LENGTH_LONG).show();

}

public void saveQuestionListener (View v){

    q[j].questionquizz=Questions.getText().toString();
    listQuestion.add(q[j].questionquizz);
    adapter.notifyDataSetChanged();

    Reponses.setVisibility(View.VISIBLE);
    radios.setVisibility(View.VISIBLE);
    SaveReponse.setVisibility(View.VISIBLE);
    SaveQuestion.setVisibility(View.VISIBLE);
    NextQuestion.setVisibility(View.VISIBLE);
    ListSaisi.setVisibility(View.VISIBLE);

    Questions.setVisibility(View.INVISIBLE);
    SaveQuestion.setVisibility(View.INVISIBLE);

    Log.d("AddQuizz", "Question est " + q[j].questionquizz);
    Toast.makeText(this, "Question est" + q[j].questionquizz, Toast.LENGTH_LONG).show();

}

public void saveReponseListener (View v){

    if (radios.getCheckedRadioButtonId()==R.id.VraiRadioButton){
        q[j].vraireponse=Reponses.getText().toString();
        Log.d("AddQuizz", "TrueReponse=" + q[j].vraireponse);
        Toast.makeText(this,"TrueReponse="+q[j].vraireponse,Toast.LENGTH_LONG).show();

        q[j].reponse_q[i]=Reponses.getText().toString();
        i++;
        Toast.makeText(AddQuizz.this, "La Vrai reponse est saisi.", Toast.LENGTH_SHORT).show();
    }
   else if (radios.getCheckedRadioButtonId()==R.id.FauxRadioButton){
        q[j].reponse_q[i]=Reponses.getText().toString();
        i++;
        Toast.makeText(AddQuizz.this, "Une Fausse reponse est saisi.", Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(AddQuizz.this, "Choisissez entre Faux reponse ou Vrai reponse.", Toast.LENGTH_SHORT).show();
    }

}
public void buttonQuestionNextListener(View v){

    //q[j]=new Question(Questions.getText().toString(),);

    db.insertData(q[j].questionquizz, q[j].reponse_q, q[j].vraireponse, q[j].NomQuizz);
    j++;
    db.chargerLesQuestions(listQuestion);
    adapter.notifyDataSetChanged();

}
public void endListener (View v){
    finish();
}
}

My Question class:

public class Question {
public String NomQuizz;
public String questionquizz;
public String [] reponse_q= new String[7];
public String vraireponse;


public Question(String question, String [] reponse_q, String val, String nom)

{
        this.questionquizz=question;
        this.reponse_q=reponse_q;
        this.vraireponse=val;
        this.NomQuizz=nom;
    }

}

Aucun commentaire:

Enregistrer un commentaire