I am trying to extract specific data from a text file and obtain the first and second line from that second line.
I appended the textfile into an arraylist and I'm trying to find the specific data's index and add +1 to find the values I want.
I then alter those values.
When I try to write the values back into the textfile I keep getting "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException".
This is my code.
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();
}
}
}
}