Getting a NullPointerException for this snippet, specifically at the "if" statement line. I have initialized and giving values to each of the variables, I cannot for the life of me figure out what is wrong.
public boolean gradeQuiz(char[] studentAnswers)
{
int l = 0;
boolean passedQuiz = false;
missedQuestions = new int[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++)
{
if (studentAnswers[i] == correctAnswers[i])
{
l++;
missedQuestions[l] = i + 1;
correct++;
}
Use a debugger and inspect the values of studentAnswers and correctAnswers on every iteration of the loop. Even better, if you have an IDE that supports exception breakpoints, add one for NullPointerException on the "if" line.
There are only 2 ways that line can generate an NPE, which are when the array variables are dereferenced. So somehow you're either forgetting to set one of them, or setting it to null midway through execution.
Name:
Anonymous2010-07-13 22:32
>correct
>correctAnswers
Where were these declared?
Name:
Anonymous2010-07-13 22:46
>>3 >>2 >>4
Thanks for the responses. Yes, I did declare correct and correctAnswers.
The class that this code is from is supposed to be used by another program, and this program will have the values for the studentAnswers array. I created a false studentAnswers array to try to work around the NPE, but it had no effect. I am trying to accept studentAnswers from the other class, but I am not completely sure how to do this (this is most likely the reason behind the NPE).
• Shit coding style
• Vague description of problem
• Incomplete source code
• Java
One can only conclude that this is a trollpost, and so in conclusion, >>1, please get out.
Name:
Anonymous2010-07-13 22:56
>>6
I'm sorry if my coding style isn't quite up to par and I doubt you would want to see all 152 lines of code. Its a vauge description of the problem because I cannot really figure out what's wrong, especially because I thought I was above getting NPEs, but obviously I was wrong. Java is very relevant and useful language.
Name:
Anonymous2010-07-13 23:01
>>6
>[quote]One can only conclude that this is a trollpost[/quote]
>>14
Richard Stallman touched my junk liberally. he strapped me in to his HURD and he couldnt keep his offensive hands off of me. he was performing many red flag touches. i couldnt believe what the fuck was going on. i told RMS the open source community would not approve of one of the leading free-software figures touching an underage kid for free.