lundi 21 décembre 2015

I want to select data from table but its giving me error.. [duplicate]

This question already has an answer here:

Here is function of DB helper class

     publicList<Question> slectQuestionsC(String tab_name)  {
                    List<Question> quesList = new ArrayList<Question>();
                   Question Quiz = new Question();
                   Quiz.setQUIZNAME(tab_name);
                  SQLiteDatabase db = this.getWritableDatabase();

               String selectQuery = "SELECT  * FROM " + Quiz.getQUIZNAME(); 

//  dbase = openOrCreateDatabase(DB, Context.MODE_PRIVATE, null);

    try {
    Cursor cursor = db.rawQuery(selectQuery, null);
    // looping through all rows and adding to list

    if (cursor.moveToFirst()) {
        do {
            //QuizMaker quest = new QuizMaker();
            Quiz.setID(cursor.getInt(0));
            Quiz.setQUESTION(cursor.getString(1));
            Quiz.setOPT1(cursor.getString(2));

            Quiz.setOPT2(cursor.getString(3));              
            Quiz.setOPT3(cursor.getString(4));
            Quiz.setOPT4(cursor.getString(5));

            Quiz.setANSWER(cursor.getString(6));
            Quiz.setHINT(cursor.getString(7));

            quesList.add(Quiz);

        } while (cursor.moveToNext());

    }

    } catch (Exception e) { e.printStackTrace(); }

    return quesList;
}

Here Main Activity its giving me null pointer error.. Althrough I have properly inserted in data base from another activity.. I have checked it from fetching data base file

       public class CustomQuizActivity extends Activity implements OnClickListener {
              public CustomQuizActivity() {
    // TODO Auto-generated constructor stub
}

Default_Front d = new Default_Front();
Question currentQ;
List<Question> quesList;
SQLiteDatabase dbase;
DataBaseHelper db = new DataBaseHelper(this);

int Question_limit ;
public String tab_name;
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.quizactivity_custom);

   db.Open();   // It will open database

    Bundle s = getIntent().getExtras();
    tab_name = s.getString("QuizName");


    try {
        quesList = db.slectQuestionsC(tab_name);
        currentQ = quesList.get(qid);
    } catch (Throwable e) {
        e.printStackTrace();
        Toast ("Exception" + e.toString());
    } 
    question_no = (TextView)findViewById(R.id.time);
    question_text = (TextView)findViewById(R.id.question_txt);
    date = (TextView)findViewById(R.id.textView1);

    question_text.setText(tab_name);


    options = (RadioGroup)findViewById(R.id.radioGroup1);
    next = (Button)findViewById(R.id.next);
    hint = (Button)findViewById(R.id.hint);

    hint.setOnClickListener(this);


    op1 = (RadioButton)findViewById(R.id.op1);
    op2 = (RadioButton)findViewById(R.id.op2);
    op3 = (RadioButton)findViewById(R.id.op3);
    op4 = (RadioButton)findViewById(R.id.op4);

    Question_limit = db.rowcount(tab_name);


    try {
    setQuestionView(); } catch (Exception e) {
        e.printStackTrace();
        Toast("Exception" + e.toString());
    }

    next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int ansID = options.getCheckedRadioButtonId();
            RadioButton rb_ans = (RadioButton)findViewById(ansID);
            String answer = rb_ans.getText().toString();
            Log.d("yourans", currentQ.getANSWER()+" "+answer);


            if(currentQ.getANSWER().equals(answer))
            {
                scoreT++;
                Log.d("score", "Your score"+scoreT);
            }

            if (qid < Question_limit)
            {
                try {
                currentQ=quesList.get(qid);
                setQuestionView();
                } catch (Throwable e) {
                    e.printStackTrace();
                    Toast("Exception" + e.toString());
                }

            }
            else 
            {
                qid = 1;
                Intent intent = new Intent(CustomQuizActivity .this, Quiz_End.class);
                Bundle b = new Bundle();
                b.putInt("score", scoreT); //Your score
                intent.putExtras(b); //Put your score to your next Intent
                startActivity(intent);
            }


        }
     });

}



@Override
public void onClick(View v) {
setHintView();

}

// =========================================================================================//

**@SuppressLint("SimpleDateFormat") private void setQuestionView()
{

    question_no.setText("Question #" + qid);
    question_text.setText(currentQ.getQUESTION());
    op1.setText(currentQ.getOPT1());
    op2.setText(currentQ.getOPT2());
    op3.setText(currentQ.getOPT3());
    op4.setText(currentQ.getOPT4());

    SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
    String currentTime = sdf2.format(new Date());


    location.setText("Time: " + currentTime);
//  date.setText("DATE: "+ currentDate);
    qid++;

}** 

@SuppressWarnings("static-access")
private void setHintView()
{
    String hinttext = currentQ.getHINT();

    if (d.hint_switch == 1)
    {
//  hint_text.setText(currentQ.getHINT());
    Toast (hinttext);
    }
    else
    {
        Toast("Hint is disabled");
    }
} 

}

Aucun commentaire:

Enregistrer un commentaire