文件不保存

时间:2015-08-24 16:59:11

标签: java file class properties

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

 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)

1 个答案:

答案 0 :(得分:1)

当您检查custnameTF.getText()时,您实际上并未检查它是否为空。空字符串和空字符串之间存在差异。添加像custnameTF.getText() != null这样的检查,它应该抛出你编写的异常而不是通过空指针。