Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:15:17+00:00 2026-06-10T23:15:17+00:00

Is this method correct? public List<Question> getQuestionSet(int i){ List<Question> questionSet = new ArrayList<Question>(); Cursor

  • 0

Is this method correct?

public List<Question> getQuestionSet(int i){
        List<Question> questionSet = new ArrayList<Question>();
       Cursor c = db.rawQuery("SELECT * FROM quiztable ORDER BY RANDOM() LIMIT X" + i, null);
        while (c.moveToNext()){
            Question q = new Question();
            q.setQuestion(c.getString(1));
            q.setAnswer(c.getString(2));
            q.setOption1(c.getString(3));
            q.setOption2(c.getString(4));
            questionSet.add(q);
        }
        return questionSet;

If yes, how do i create a method of displaying each question from my database individually in random order.. sorry. i have no idea what to do…

EDIT:
I tried changing the code above to your code. Now another question. This is the code i have in my MainActivity file:

public class Second extends Activity {

TextView questionTextView;
DBAdapter db = new DBAdapter(this);
Cursor mCursor;



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

    TextView questionTextView = (TextView) findViewById(R.id.question);
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
    RadioButton rb0 = (RadioButton) findViewById(R.id.radio0);
    RadioButton rb1 = (RadioButton) findViewById(R.id.radio1);
    RadioButton rb2 = (RadioButton) findViewById(R.id.radio2);

    db.open();
    db.getQuestionSet(1);
    String question = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_QUESTIONS));
    String answer = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_ANSWERS));
    String opt1 = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_CHOICE1));
    String opt2 = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_CHOICE2));
    questionTextView.setText(question);
    rb0.setText(answer);
    rb1.setText(opt1);
    rb2.setText(opt2);

}

}

I get an error when i try to run it. i know my code is incorrect. but i am not sure how to correct it. To be more specific, what i am trying to do is create an app quiz. I managed to create a database. I could insert/update/delete questions and answers thru the application. But i can’t display the questions stored in my database so i could answer the questions and grade it. thank you for your help guys. Im a beginner. been studying eclipse for a month now.. and i cant even create a simple quiz app. 🙁

Thank you. Here is the error shown in Logcat:

 09-03 15:29:49.516: D/AndroidRuntime(4091): Shutting down VM
 09-03 15:29:49.516: W/dalvikvm(4091): threadid=1: thread exiting with uncaught exception (group=0x40015560)
 09-03 15:29:49.527: E/AndroidRuntime(4091): FATAL EXCEPTION: main
 09-03 15:29:49.527: E/AndroidRuntime(4091): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dtan.quiz/com.dtan.quiz.Second}: java.lang.NullPointerException
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.os.Handler.dispatchMessage(Handler.java:99)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.os.Looper.loop(Looper.java:123)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.ActivityThread.main(ActivityThread.java:3683)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at java.lang.reflect.Method.invokeNative(Native Method)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at   java.lang.reflect.Method.invoke(Method.java:507)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at dalvik.system.NativeStart.main(Native Method)
 09-03 15:29:49.527: E/AndroidRuntime(4091): Caused by: java.lang.NullPointerException
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at com.dtan.quiz.Second.onCreate(Second.java:30)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
 09-03 15:29:49.527: E/AndroidRuntime(4091):    ... 11 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:15:18+00:00Added an answer on June 10, 2026 at 11:15 pm
    public List<Question> getQuestionSet(int i) {
        List<Question> questionSet = new ArrayList<Question>();
        final Cursor c = db.rawQuery("SELECT * FROM quiztable ORDER BY RANDOM() LIMIT " + i, null);
        if (c != null) {
            try {
                while (c.moveToNext()) {
                    Question q = new Question();
                    q.setQuestion(c.getString(1));
                    q.setAnswer(c.getString(2));
                    q.setOption1(c.getString(3));
                    q.setOption2(c.getString(4));
                    questionSet.add(q);
                }
            } finally {
                c.close();
            }
        }
        return questionSet;
    }
    

    EDIT:

    You have not initialized your Cursor. But it’s useless as you are returning List from your getQuestionSet() method, use it instead.

    List<Question> questions = db.getQuestionSet(1);
    if (questions != null && !questions.isEmpty()) {
       Question question = questions.get(0);
       now use:
       question.getQuestion();
       question.getAnswer();
       question.getOption1();
       question.getOption2();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: list.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int
Having this method: public Boolean isCorrect() { return correct; } I can't write: @XmlType(propOrder
This is follow on from this question: Format List<T> to concatenate fields . The
This method right below reverses a doubly linked list with n elements. I dont
Take this method /** * @return List of group IDs the person belongs to
Can this method work? public String sayHello(){ return Hello.jsp?name= + laala; } I am
(Update - from comments) Question: Is there any advantage of using one extension method
this question is following this one . This is the new web.xml: <?xml version=1.0
This method works as expected - it creates a JTree with a root node
This method that draws my tiles seems to be quite slow, Im not sure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.