Search code examples
javabufferedreader

Java BufferedReader error: NullPointerException


I have a small assignment for uni which I seem to be stuck with. The application is suppose to be a quiz program which reads questions and answers from a text files and stores them like a flash card. My problem is that my buffered reader seems to be returning the nullPointer exception when it tries to read from the file. I'm unsure why this is. I will provide all code and highlight the error in bold. After doing a bit of debugging I found that the readLine method was returning null. Any thoughts? Thanks a lot. Error is at String[] line = getLine().split(":");

text file is in the format question:answer

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Quiz {
    private ArrayList<FlashCard> flashCards;
    public static void main(String[] args){
        Quiz quiz1 = new Quiz();
    }
    public Quiz(){
        FlashCardReader cardReader = new FlashCardReader();
        try {
            if(cardReader.isReady()==true){
            flashCards = cardReader.getFlashCards();
            play();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void play(){
        Scanner userInput = new Scanner(System.in);
        String answer = userInput.nextLine();
        for(FlashCard card: flashCards){

            System.out.println(card.getQuestion());
            System.out.println("********************");
            sleep(10000);
            System.out.println("Enter your answer:");
            answer = userInput.nextLine();
            if(card.getAnswer() == answer){
                System.out.println("Correct.");
            }else{
                System.out.println("Incorrect. The correct answer is " +     card.getAnswer() + ".");
            }

        }
    }
    private void sleep(int x){
        try {
            Thread.sleep(x);
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
}




import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class FlashCardReader {
    BufferedReader reader;
    public FlashCardReader(){
        try {
            reader = new BufferedReader(new FileReader("Questions.txt"));
        } catch (FileNotFoundException e) {
            System.err.println(e.toString());
        }


    }
    public String getLine() throws IOException{
        return reader.readLine();
    }
    public Boolean isReady() throws IOException{
        return reader.ready();
    }
    public ArrayList<FlashCard> getFlashCards(){
        ArrayList<FlashCard> flashcards = new ArrayList<FlashCard>();
        try {
            for(int i = 1; i <= reader.lines().count(); i++){
                **String[] line = getLine().split(":");**
                System.out.println(line[0]);
                flashcards.add(new FlashCard(line[0],line[1]));
            }
        } catch (IOException e) {
            System.err.println(e);
            e.printStackTrace();
        }
            return flashcards;

    }
}




public class FlashCard {
    private String question;
    private String answer;
    public FlashCard(String question, String answer){
        this.question = question;
        this.answer = answer;
    }
    public String getQuestion(){
        return question;
    }
    public String getAnswer(){
        return answer;
    }
}

Solution

  • How about changing the for loop to while loop to avoid null condition. As you cannot be sure that your code starts from line one as you expect by assigning i to 1.

    String lines ;
    while((lines = getLine()) != null){
                    String[] lineArray = lines.split(":");
                    System.out.println(lineArray[0]);
                    flashcards.add(new FlashCard(lineArray[0],lineArray[1]));
                }