| Author |
need help with 2 bugs
|
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4089
|
|
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
|
 |
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4089
|
|
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: 4089
|
|
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: 4089
|
|
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: 4089
|
|
|
woohoo it works! If anyone is curious I had to put the offending line inside an ItemListener(instead of while loop)
|
 |
 |
|
|
subject: need help with 2 bugs
|
|
|