有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用Java保存文件

因此,我试图将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事件队列将我抛出。当我看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;
    }

保存新文件的My 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。什么也救不了size随着元素添加到add函数中的data[]而增加。因此size是数组data[]中的元素数


共 (1) 个答案

  1. # 1 楼答案

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

    答:因为给定提供的代码,数据[]为空

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

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

    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事件发生异常(按键)