【问题标题】:Scanning a text file and storing it in an array list is giving a null pointer exception?扫描文本文件并将其存储在数组列表中会出现空指针异常吗?
【发布时间】:2014-04-03 11:47:27
【问题描述】:

您好,我正在尝试读取从文本框中获取的文本文件并在不同的类中读取它。我应该输出该文本文件的第一类 Gui 给了我一个错误。

public class Gui {
private static JFrame frame;
private static JToolBar toolbar;
private static JTextField input;
private static JTextArea output;
private static JScrollPane scrollPane;
private static JButton read, clear;
public static ArrayList<String> Lines;
private static final int HOR_SIZE = 400;
private static final int VER_SIZE = 150;
public static reader reader = new reader();
private static File file;
private static ArrayList<String> temp = new ArrayList<>();

public static void initComponents(){

    frame = new JFrame("CS-371 - File Reader");
    frame.setSize(400, 400);
    toolbar = new JToolBar();
    input = new JTextField();
    read = new JButton("Read File");
    clear = new JButton("Clear");
    output = new JTextArea();
    output.setSize(HOR_SIZE, VER_SIZE);
    //output.setEditable(false);
    scrollPane = new JScrollPane(output);           
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setMaximumSize(new java.awt.Dimension(HOR_SIZE, VER_SIZE));
    scrollPane.setMinimumSize(new java.awt.Dimension(HOR_SIZE, VER_SIZE));
    scrollPane.setPreferredSize(new java.awt.Dimension(HOR_SIZE, VER_SIZE));


    toolbar.add(input);
    toolbar.add(read);
    frame.add(scrollPane, BorderLayout.CENTER );
    frame.add(toolbar,BorderLayout.NORTH);
    frame.add(clear, BorderLayout.SOUTH);
    toolbar.setFloatable(false);
    frame.setVisible(true);
}

public static void main(String[] args){

    initComponents();
    read.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            reader.setPath(input.getText());
            file = new File(input.getText());
            if(!file.isFile()){
                output.setText(reader.checkPath()); 
            }else{
                temp = reader.getFile();
                if(!temp.isEmpty()){
                    for(int i = 0; i < temp.size(); i++){
                        output.setText(temp.get(i).toString()); 
                    }
                }else{
                    output.setText("File empty");
                }
            }
        }

    }); 
    clear.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            output.setText("");
        }
    });
}
}

在 temp = reader.getFile() 行,这是一个设置的数组列表,它从我的阅读器类中的方法获取其内容。

public class reader implements Runnable {
     private Scanner scanner;
     private File file;
     private String path;
     private ArrayList<String> fileContents;
public String getLine(){

    return null;

}
public void setPath(String path){
        this.path = path;
}
public String checkPath(){
    file = new File(path);
    if(file.isFile()){

    }else{
        return "File not found";
    }
    return "";
}
public ArrayList<String> getFile(){
    file = new File(path);
    try {
        scanner = new Scanner(file);
        String scan = scanner.nextLine();
        fileContents.add(scan);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return fileContents;
}
@Override
public void run() {


}
}

我从这个类得到的错误是 fileContents.add(scan) 两者显然都给出了空指针异常,但我不知道为什么应该填充我的数组列表,然后另一个数组列表在将其内容设置为它调用的返回数组列表时应该没有问题

【问题讨论】:

    标签: java arrays file filereader


    【解决方案1】:

    你应该首先初始化fileContents如下:

    private ArrayList<String> fileContents = new ArrayList<String>();
    

    否则默认为null

    【讨论】:

    • 谢谢,不敢相信我错过了它让我发疯!无论如何,谢谢它现在可以工作了!
    【解决方案2】:

    您需要初始化您的ArrayList。也就是说,实际上是在某个时候创建它

    你可以直接在它的声明上做,像这样:

    private final List<String> fileContents = new ArrayList<String>();
    

    或使用构造函数

    public reader() {
        this.fileContents = new ArrayList<String>();
    }
    

    附带说明,Java 中类名大写的约定。因此,您的reader 类可以(应该?)被称为Reader
    实际上,我还建议您尽可能避免为您的类使用此标识符,因为 Java 标准库中已经存在一个具有相同名称的接口

    【讨论】:

    • 是的,这就是我将类名小写以避免类似情况的原因,我将在提交之前更改类名。并感谢初始化数组列表的工作:)
    【解决方案3】:
    ...
    private ArrayList<String> fileContents = new ArrayList<>();
    ...
    

    【讨论】:

    • 是的,谢谢,现在我脑筋急转弯忘记初始化了:\
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 2015-04-01
    • 2014-02-14
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    相关资源
    最近更新 更多