遍历文件数组

Iterating through an array of files

我正在编写一个程序,将我写的一些诗显示到 JTextArea 中,每当我按下 JButton "Next" 时,它应该将我的另一首诗读入 JTextArea

但是我只能在删除 filearray 并只读取单个文件时才能这样做。

创建文件数组并尝试在每次单击下一步时调用 number++ 进行迭代。我还通过遗漏一些不相关的代码进行了过滤。

public class PoemWindow extends JFrame implements ActionListener
{
    private int n=0;
    BufferedReader br;
    JTextArea textAreaOne;
    FileReader file[];
}

public PoemWindow() throws IOException{
    file[0] = new FileReader("C:/Poems/Freedom.txt");
    file[1] = new FileReader("C:/Poems/Masturbation.txt");
    file[2] = new FileReader("C:/Poems/Life.txt");
    file[3] = new FileReader("C:/Poems/Loneliness.txt");
    add(textAreaOne)
}

public void actionPerformed(ActionEvent e){
    if(e.getSource()==nextButton) {
        while(n<=4) {   
            try {
                br = new BufferedReader(file[n]);
                textAreaOne.read(br,null);
                n++;
                textAreaOne.requestFocus();
            } catch(Exception b) {
                System.out.println("This is an error"); 
            }
        }
    }

它在我读取单个文件时有效,但每当我实例化一个文件时[] 它给了我一个 NullPointerException 而它甚至不会 运行...

It works when I read a single file, but whenever I instantiate an file [] it gives me an NullPointerException and it won't even run...

下面是创建带有五个槽 (0 --> 4) 的数组的示例:

FileReader[] file = new FileReader[4];