空指针异常字符串构建器

时间:2014-05-06 22:32:39

标签: java stringbuilder

我试图在StringBuilder中使用setCharAt方法,但我得到一个空指针异常。有没有办法我可以为我所做的StringBuilder数组添加值,所以我不会得到这些错误。

从研究中我发现了.append()方法,但我甚至不确定它是如何工作的。

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;


}

}

错误发生在第91行:

wrongAnswers[i].setCharAt(charIndex, 'X');

3 个答案:

答案 0 :(得分:1)

StringBuilder[] wrongAnswers = new StringBuilder[5];

不会创建5个空StringBuilders,而是5个null StringBuilders。

您需要调用类似

的内容

wrongAnswers[i] = new StringBuilder()

为了初始化你的5个数组成员。

答案 1 :(得分:1)

您已声明StringBuilder个数组,但尚未初始化任何广告位,因此它们仍为null

初始化它们:

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

此外,使用setCharAt在此无法使用,因为最初,StringBuilder中没有任何内容。根据您的需要,您可能需要拨打append,或者您最初可能需要一个充满空格的字符串,以便您可以将特定字符设置为'X'

答案 2 :(得分:1)

你的问题是那个

StringBuilder[] wrongAnswers = new StringBuilder[5];

不会创建5个StringBuilder个对象。它只创建一个包含5个空StringBuilder引用的数组。您需要使用

等行分别创建每个StringBuilder
wrongAnswers[i] = new StringBuilder();

i之上的循环中。