aspose file tools*
The moose likes Java in General and the fly likes need help with 2 bugs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "need help with 2 bugs" Watch "need help with 2 bugs" New topic
Author

need help with 2 bugs

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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).]


SCJP
Visit my download page
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Oct 21, 2000
Posts: 4347
    
    2

woohoo it works! If anyone is curious I had to put the offending line inside an ItemListener(instead of while loop)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: need help with 2 bugs