• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help with 2 bugs

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Applet has 2 problems. Both are in the displayQuiz method.
1) This line doesnt seem to work(81).
answers = (String[])theAnswers.elementAt(i);
The array answers doesnt seem to get reassigned. Are Arrays immutable?
2) A NullPointerException is thrown from this line(104).
if(((cbg.getSelectedCheckbox()).getLabel()).equals(correctAnswer)) {
here is the code:

</BLOCKQUOTE>

[This message has been edited by Randall Twede (edited February 07, 2001).]
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the code making the String[] answers local in both loadQuiz and displayQuiz methods. But I get the same results. No matter which question is displayed the last set of answers is shown. I am lost.
I tried changing:
theQuestions.add(question); theAnswers.add(answers);
to
theQuestions.addElement(question); theAnswers.addElement(answers);
Same problem. Although add() is new to 1.2 and would have caused a problem with browsers.

</BLOCKQUOTE>
[This message has been edited by Randall Twede (edited February 07, 2001).]
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I found the problem with the array. The Vector hold a reference to the array not the array itself. I moved this line String[] answers = new String[4]; inside the while loop. I now get the correct set of answers.
I still get a NullPointerException though at this line:
if(((cbg.getSelectedCheckbox()).getLabel()).equals(correctAnswer)) {
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried changing this:
for(j = 0;j < 4;j++) {
choices.add(new Checkbox(answers[j], cbg, false));
}
to this:
Checkbox cb1 = new Checkbox(answers[0], cbg, false);
Checkbox cb2 = new Checkbox(answers[1], cbg, false);
Checkbox cb3 = new Checkbox(answers[2], cbg, false);
Checkbox cb4 = new Checkbox(answers[3], cbg, false);
choices.add(cb1);
choices.add(cb2);
choices.add(cb3);
choices.add(cb4);
but still a NullPointerException here:
if(((cbg.getSelectedCheckbox()).getLabel()).equals(correctAnswer)) {
even if i choose the right answer it says incorrect
the correct answer is "correctAnswer"
[This message has been edited by Randall Twede (edited February 07, 2001).]
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
woohoo it works! If anyone is curious I had to put the offending line inside an ItemListener(instead of while loop)
 
reply
    Bookmark Topic Watch Topic
  • New Topic