【问题标题】:Java BufferedReader error: NullPointerException [duplicate]Java BufferedReader 错误:NullPointerException [重复]
【发布时间】:2015-11-29 11:50:45
【问题描述】:

我有一个小任务,我似乎被困住了。该应用程序应该是一个测验程序,它从文本文件中读取问题和答案,并像闪存卡一样存储它们。我的问题是我的缓冲阅读器在尝试从文件中读取时似乎返回了 nullPointer 异常。我不确定这是为什么。我将提供所有代码并以粗体突出显示错误。在做了一些调试后,我发现 readLine 方法返回 null。有什么想法吗?非常感谢。错误在 String[] line = getLine().split(":");

文本文件格式为 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;
    }
}

【问题讨论】:

  • 之前获取并测试getline。也许它的空:)
  • 我刚刚打印了两次 getline 并打印了前两个问题的答案,所以现在我想知道为什么当我调用 .split 时它返回 null?
  • 不要将布尔值与true 进行比较,也不要将字符串与== 进行比较。

标签: java bufferedreader


【解决方案1】:

如何将 for 循环更改为 while 循环以避免空条件。因为您无法通过将 i 分配给 1 来确定您的代码是否从第一行开始。

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

【讨论】:

  • 非常感谢,现在都已修复:)
  • @darkjoe31 .. 干杯 ..!!很高兴我能帮上忙
猜你喜欢
  • 1970-01-01
  • 2019-01-05
  • 2016-04-03
  • 2015-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多