java - 文件未保存

标签 java file class properties

任何人都可以指出为什么它不让我保存我的预订文件吗?在尝试保存客户名称后,它给了我一个异常(exception)。也看不到任何错误,所以不知道到底出了什么问题。谢谢

 private void savebookingButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        Booking customerbooking = new Booking();
        Customer customerfile = customerbooking.getCustomer();

        try {
            if (custnameTF.getText().equals("")) {
                throw new EmptyField("Please Insert Customer");
            } else {
                FileOutputStream fos = new FileOutputStream("Bookings/" + custidTF.getText() + ".txt");
                ObjectOutputStream oos = new ObjectOutputStream(fos);

                customerfile.setPersonName((custnameTF.getText()));
                customerfile.setPersonSurname((custsurnameTF.getText()));
                customerfile.setPersonID((custidTF.getText()));
                customerfile.setConsultantname(consnameTF.getText());
                customerfile.setConsultantsurname((conssurnameTF.getText()));
                customerfile.setConsulid(considTF.getText());
                customerbooking.setFlightlocation(locationCB.getSelectedItem().toString());
                customerbooking.setFlighttime(timeCB.getSelectedItem().toString());
                customerbooking.setFlightfee(feeCB.getSelectedItem().toString());
                customerbooking.setCar(carRB.isSelected());
                customerbooking.setInsurance(insuranceRB.isSelected());

                oos.writeObject(customerbooking);
                oos.close();
                fos.close();

                custnameTF.setText("");
                custsurnameTF.setText("");
                custidTF.setText("");
                considTF.setText("");
                consnameTF.setText("");
                conssurnameTF.setText("");
                locationCB.setSelectedItem("");
                timeCB.setSelectedItem("");
                feeCB.setSelectedItem("");

                JOptionPane.showMessageDialog(this, "Booking was Saved Successfully!",
                        "Success", JOptionPane.INFORMATION_MESSAGE);
            }

        } catch (IOException e) {
            JOptionPane.showMessageDialog(this, "Booking could not be Saved!",
                    "Error!", JOptionPane.INFORMATION_MESSAGE);
        } catch (EmptyField ex) {
            JOptionPane.showMessageDialog(this, "Please Insert Customer",
                    "Error", JOptionPane.INFORMATION_MESSAGE);
        }

        dispose();

    }                                  
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at airline.booking.system.BookingFrame.savebookingButtonActionPerformed(BookingFrame.java:357)
    at airline.booking.system.BookingFrame.access$200(BookingFrame.java:21)
    at airline.booking.system.BookingFrame$3.actionPerformed(BookingFrame.java:102)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:702)
    at java.awt.EventQueue$3.run(EventQueue.java:696)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:724)
    at java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

最佳答案

当您检查custnameTF.getText()时,您实际上并没有检查它是否为空。空字符串和空字符串之间是有区别的。添加像 custnameTF.getText() != null 这样的检查,这应该抛出您编写的异常,而不是进入空指针。

关于java - 文件未保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32187741/

相关文章:

java - 在同一个类中实现多个迭代器

Python:复制路径中带有特殊字符的文件

PHP将文本文件中的解析值插入MySQL DB

c - C/C++ 的独立跨平台 (Windows/Linux) 文件压缩?

c++ - 如何更改集合中的对象子成员

java - ClassCastException:datastructs.instances.JClass 无法转换为 java.util.ArrayList

java - 整数数组排列算法

java - 当 Object 是 List 时替换 List<Object> 中的对象? java

java - 如何查找一个方法中调用的所有方法?

java - 如何从 JTable 中获取第一个小时