0

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();
            }
     }  

}

}

andih
  • 5,570
  • 3
  • 26
  • 36
  • Could you please add an example of the input and the expected output file. – SubOptimal Apr 27 '17 at 07:42
  • So this is a scoring system where youre score is altered if you are not a new user, you can get between 1,5 so If the person is a new user their name is saved like this; John Smith *Number of Correct Answers* *Number of questions asked* – Kuoraf Ila Apr 27 '17 at 07:46

0 Answers0