用Java保存文件

时间:2014-11-25 18:33:46

标签: java save jfilechooser

所以,我试图将我的java程序中创建的数据保存到用户使用JFILECHOOSER选择的新文件中。我得到了程序来选择保存文件,但是当它将数据写入文件时,我收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at q1.Heaps.countOccurance(Heaps.java:131)
    at q1.Heaps.save(Heaps.java:155)
    at q1.createGui$Save.actionPerformed(Gui.java:219)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

从这看起来,错误发生在countOccurance方法中,但随后是AWT-EventQueue抛出了我。当我看到countOccurance时,没有我能看到的错误,一切看起来都不错。所以我不知道为什么我会收到错误,除非它与NullPointerException

有关

堆类:

public int countOccurance(Comparable data2){
        int count = 0;
        for (int i =0; i < size; i++){
            if(data[i].equals(data2)){
                count++;
            }
        }
        return count;
    }

public boolean save(File file) throws FileNotFoundException{
        if(isEmpty()){
            return false;
        }

        PrintWriter writer = new PrintWriter(file);
        for(int i =0; i < size; i++){
            writer.println(data[i] + " occurs " + countOccurance(data[i]) + " time(s)");
        }
        writer.close();
        return true;
    }

我的Actionlistner保存新文件:

class Save implements ActionListener {
    public void actionPerformed(ActionEvent e){
        JFileChooser file = new JFileChooser("Save");
        if (file.showSaveDialog(null)==JFileChooser.APPROVE_OPTION){
            try {
            boolean done = heap.save(file.getSelectedFile());
            if(done){
                infofield.append("Save done to " + file.getSelectedFile());
            }else{
                infofield.append("Save failed: Heap might be empty");
            }

            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
        }
    }
}

data[]在保存之前已初始化,否则size将为0.无法保存任何内容。 sizedata[]函数中添加到add时会增加size。因此data[]是数组{{1}}中元素的数量。

1 个答案:

答案 0 :(得分:0)

问题(据我所知)是:为什么我得到NullPointerExcepttion

答案:因为,根据提供的代码,data []为空。

这怎么不是问题的答案?尽管编辑,仍然是我的答案。如果这不作为答案删除,我将很乐意详细说明。

请注意,整个数据数组不必为空。可能是被测元素(data [i])可能为null。避免异常的一种方法是更改​​代码,如下所示。

public int countOccurance(Comparable data2){
    int count = 0;
    for (int i =0; i < size; i++){
        if(data[i] != null && data[i].equals(data2)){
            count++;
        }
    }
    return count;
}

这将忽略数据中的空值,并仅计算非空值。

AWT-EventQueue出现在堆栈跟踪中,因为AWT事件发生异常(按键)