我正在尝试从文本文件中提取特定数据,并从第二行获取第一行和第二行。
我将文本文件附加到arraylist中,我正在尝试查找特定数据的索引并添加+1以查找我想要的值。
然后我改变了这些值。当我尝试将值写回文本文件时,我不断得到“线程中的异常” AWT-EventQueue-0“java.lang.NullPointerException”。
这是我的代码。
public class AppendsScore extends quiz{
public void AppendsScore() {
String filename = "Data_CS";
FileWriter fw = null;
try {
fw = new FileWriter(filename, true);
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
List<String> Score = new ArrayList<String>();
Integer CorrectAnswers = quiz_questions.CorrectAnswers;
{
try (BufferedReader br = new BufferedReader(new FileReader(filename))){
String line=br.readLine();
//System.out.printf(FName + SName);
while (line != null) {
Score.add(line);
line=br.readLine();
System.out.print(Score);
}
int name = Score.indexOf(FName + " " + SName);
int correct = 1 + name;
int NoofQuestions = 2 + name;
Integer CorrectInt = Integer.parseInt(Score.get(correct));
CorrectInt = CorrectInt + CorrectAnswers;
String CorrectStr = Integer.toString(CorrectInt); //To String from Double
Integer NoQuestionsInt = Integer.parseInt(Score.get(NoofQuestions));
NoQuestionsInt = NoQuestionsInt + CorrectAnswers;
String NoQuestionsStr = Integer.toString(NoQuestionsInt);
Score.set(correct , CorrectStr );
Score.set(NoofQuestions , NoQuestionsStr );
for(String str: Score) {
fw.write(str);
}
fw.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}