Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java time!

Name: Roy 2010-07-13 22:17

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++;
            }

Name: Anonymous 2010-07-13 22:29

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.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List