1

I am trying to use the setCharAt method in a StringBuilder but I am getting a null pointer exception. Is there a way I can add values to the StringBuilder array I have made so I wont get these error.

From research I have found the .append() method but I'm not even sure how it works.

import java.util.*;  // Allows for the input of a scanner method.
import java.io.*;    // Allows for the inputting and outputting of a data file.
import java.lang.*;  // Allows for the use of String Methods.
//////////////////////////////////////////////////////////////////////////////////////

public class TESTY
{
static Scanner testanswers;
static PrintWriter testresults;

public static void main(String[] args) throws IOException
{
    testanswers = new Scanner(new FileReader("TestInput.dat"));
    testresults = new PrintWriter("TestOutput.dat");

    String StudentID;
    String answers;
    // Reads first two lines first to know how many records there are.
    String answerKey = testanswers.nextLine();
    int count = Integer.parseInt(testanswers.nextLine());

    // Allocate the array for the size needed.
    String[][] answerArray = new String[count][];

    for (int i = 0; i < count; i++)
    {
        String line = testanswers.nextLine();
        answerArray[i] = line.split(" ", 2);
    }

    for(int row = 0; row < answerArray.length; row++)
    {
        for(int col = 0; col < answerArray[row].length; col++)
        {
            System.out.print(answerArray[row][col] + " ");
        }
        System.out.println();
    }   
    gradeData(answerArray, answerKey);




    testanswers.close();
    testresults.close();

}
///////////////////////////////////////////////////////////
//Method: gradeData
//Description: This method will grade testanswers showing 
//what was missed, skipped, letter grade, and percentage.
///////////////////////////////////////////////////////////
public static double gradeData(String[][] answerArray, String answerKey)
{   

    String key = answerKey;
    double Points = 0;
    StringBuilder[] wrongAnswers = new StringBuilder[5];
    String studAnswers;

    for(int rowIndex = 0; rowIndex < answerArray.length; rowIndex++)   /// Counting rows
    {
        studAnswers = answerArray[rowIndex][1].replace(" ", "S");  ///Counts rows, Col stay static index 1
        for(int charIndex = 0; charIndex < studAnswers.length(); charIndex++) 
        {
            if(studAnswers.charAt(charIndex) == key.charAt(charIndex))
            {
                Points += 2;
            }
            else if(studAnswers.charAt(charIndex) == 'S')
            {
                Points --;
            }
            else if(studAnswers.charAt(charIndex) != key.charAt(charIndex))
            {
                for(int i = 0; i < wrongAnswers.length; i++)
                {

                    wrongAnswers[i].setCharAt(charIndex, 'X');
                }
                Points -= 2;                        
            }

        }
        System.out.println(Points);
    }

    return Points;


}

}

The error is occurring on line 91 :

wrongAnswers[i].setCharAt(charIndex, 'X');
1
  • wrongAnswers[i] is null. Initialize it first. Commented May 6, 2014 at 22:34

3 Answers 3

2

You have declared an array of StringBuilders, but you haven't initialized any of the slots, so they're still null.

Initialize them:

StringBuilder[] wrongAnswers = new StringBuilder[5];
for (int i = 0; i < wrongAnswers.length; i++)
{
    wrongAnswers[i] = new StringBuilder();
}

Additionally, using setCharAt won't work here, because initially, there is nothing in the StringBuilder. Depending on what you want here, you may need to just call append, or you may initially want a string full of spaces so that you can set a specific character to 'X'.

Sign up to request clarification or add additional context in comments.

1 Comment

After comparing my studAnswers to key, I am wanting something that will make an array of 5 elements that will replace the specific character index of an element with 'X' permitting studAnswers and key are not equal.
1

StringBuilder[] wrongAnswers = new StringBuilder[5];

does not create 5 empty StringBuilders but 5 null StringBuilders.

You need to call something like

wrongAnswers[i] = new StringBuilder()

in order to initialize your 5 array members.

Comments

1

Your problem is that

StringBuilder[] wrongAnswers = new StringBuilder[5];

does not create 5 StringBuilder objects. It only creates an array with 5 null StringBuilder references. You need to create each StringBuilder separately with a line such as

wrongAnswers[i] = new StringBuilder();

inside a loop over i.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.