File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

I am getting a NullPointerException and I dont see the problem.
Exception occurred during event dispatching:
java.lang.NullPointerException
at Quiz.displayQuiz(Quiz.java:103)
this is line 103:
if(cbg.getSelectedCheckbox().getLabel().equals(correctAnswer)) {
Here is all the code:


SCJP
Visit my download page
Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
I'm a newbie, but trying to learn. I hope this helps.
Either yout checkbox items are not being initialized with the data from your inputted data or the quiz answer is not being inputted correctly. Maybe check the database to see that they are stacked correctly or being accessed at a specific starting point??
Tell me if it works either way. I'm trying.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

I might have not chosen the best forum for this question. I can find most NullPointerException problems, but my mind is blocked on this one I guess. Maybe I need a break
One thing I do know is that the question bank I am using only has 5 lines(1 question, 4 answers) and I havent dealt with running out of questions yet. I can try adding more lines to it. It might reveal the problem. Or not.
After it throws the exception if I click the "Go" button, it says incorrect, and displays the correct answer, even if I chose the right answer. so the test returns false but correctAnswer is not null. Besides it is ok to test for null(no exception just from comparing to null).

[This message has been edited by Randall Twede (edited February 05, 2001).]
Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
int j = (int)(Math.random()*4);
String temp = answers[j];
answers[j] = answers[0];
answers[0] = temp;
correctAnswer = answers[j];
Based on the snippet above, isn't the correct answer randomly selected?
still trying....Brian
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Initially the correct answer is the first answer in the array. the algorithm is only supposed to mix them up for display purposes.
Eek! I have a logic error as well! I added a second questions and set of answers. When I ran it I not only got a NullPointerException(like before) but it displayed the wrong set of answers!
The two problems might be related. In fact they probably are. That was a clue for me.
Let this be a lesson to all beginners with a procedural programming background. Learn Object Oriented thinking or you will end up like me

[This message has been edited by Randall Twede (edited February 05, 2001).]
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Well I really mucked it up somehow. It displays the questions randomly, but it displays the same set of answers(the last ones in the file) for every question. It says the correct answer is something different each time. Although it seems to give the correct answer to the last question in the file the first time, even though it is wrong set of answers.
here is the .txt file I am using right now:
What does D&D stand for?
Dungeons and Dragons
Death and Destruction
Demons and Devils
Dirks and Daggers
D&D is a what?
Role Playing Game
Strategy Game
Card Game
Not a game at all
How many core rule books in D&D?
3
1
4
2
here is a test run:
what is D&D?
1
4
3
2
correct answer is 3
What does D&D stand for?
1
3
4
2
correct answer is 4
How many core books in D&D
3
1
4
2
correct answer is 1
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

Never mind. It seems I am going to have to rewite it using Vectors anyway. I didnt realize HashMaps are new. They are not supported by browsers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException
 
Similar Threads
Frame Attack!
Some of you are experts this could be your next problem!
need help with 2 bugs
removeAll()
Need ur suggestion... Urgent!