Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BufferedReader错误:NullPointerException_Java_Bufferedreader - Fatal编程技术网

Java BufferedReader错误:NullPointerException

Java BufferedReader错误:NullPointerException,java,bufferedreader,Java,Bufferedreader,我在大学里有一个小任务,我似乎一直在做。该应用程序被认为是一个测验程序,它从文本文件中读取问题和答案,并像闪存卡一样存储它们。我的问题是,当我的缓冲读取器尝试读取文件时,它似乎正在返回nullPointer异常。我不确定这是为什么。我将提供所有代码并以粗体突出显示错误。在进行了一点调试之后,我发现readLine方法返回null。有什么想法吗?谢谢。错误位于String[]line=getLine() 文本文件的格式为问题:答案 import java.io.IOException; impor

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

文本文件的格式为问题:答案

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;
    }
}
import java.io.IOException;
导入java.util.ArrayList;
导入java.util.Scanner;
公开课测验{
私人ArrayList抽认卡;
公共静态void main(字符串[]args){
测验quiz1=新测验();
}
公开问答{
FlashCardReader=新的FlashCardReader();
试一试{
if(cardReader.isReady()==true){
flashCards=cardReader.getFlashCards();
play();
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
私人虚空游戏(){
扫描仪用户输入=新扫描仪(System.in);
字符串answer=userInput.nextLine();
用于(抽认卡:抽认卡){
System.out.println(card.getQuestion());
System.out.println(“**********************”);
睡眠(10000);
System.out.println(“输入您的答案:”);
answer=userInput.nextLine();
if(card.getAnswer()==answer){
System.out.println(“正确”);
}否则{
System.out.println(“不正确。正确答案是“+card.getAnswer()+”);
}
}
}
私人虚空睡眠(INTX){
试一试{
睡眠(x);
}捕获(中断异常例外){
Thread.currentThread().interrupt();
}
}
}
导入java.io.BufferedReader;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
公共类读卡器{
缓冲读取器;
公共FlashCardReader(){
试一试{
reader=newbufferedreader(newfilereader(“Questions.txt”);
}catch(filenotfounde异常){
System.err.println(例如toString());
}
}
公共字符串getLine()引发IOException{
返回reader.readLine();
}
公共布尔值isReady()引发IOException{
返回reader.ready();
}
公共阵列列表getFlashCards(){
ArrayList flashcards=新的ArrayList();
试一试{

for(int i=1;i如何将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]));
            }

在之前获取并测试getline。可能为null:)我刚刚打印了两次getline,它打印了前两个问题和答案,所以现在我想知道为什么我调用时它返回null。split?不要将布尔值与
true
进行比较,也不要将字符串与
=
进行比较。非常感谢,所有问题都已解决:)@darkjoe31..干杯!!很高兴我能提供帮助