【发布时间】:2014-11-25 18:33:46
【问题描述】:
所以,我正在尝试将在我的 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。不会保存任何内容。在add 函数中将元素添加到data[] 时,size 会增加。因此size 是数组data[] 中的元素数。
【问题讨论】:
标签: java save jfilechooser