0

anyone can pin point why it is not letting me save my Booking file? It is giving me an exception after trying to save the Customer Name. Cant see any errors also so don't know exactly what is wrong. Thanks

 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)
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
Luke_i
  • 163
  • 1
  • 2
  • 13

1 Answers1

1

When you are checking custnameTF.getText() you aren't actually checking to see if it is null. There is a difference between an empty string and an null string. Add in a check like custnameTF.getText() != null and that should throw the exception you programmed instead of going through to a null pointer.

GrizzlyManBear
  • 647
  • 8
  • 16
  • Good eye! That seems like the correct answer to me. – Amos Bordowitz Aug 24 '15 at 18:06
  • It doesn't make sense to me, that this is the problem. OP said that the exception occurs here: `customerfile.setPersonName((custnameTF.getText()));`. If `custnameTF` is `null`, then why should it fail on the _second_ call? I also don't think that a TextField would return `null` for `getText`. – Tom Aug 24 '15 at 18:18
  • There is also a difference between comparing a value and using a value. If a field is null, you can still compare it to whatever you want, but you cannot set a null value to another variable. This is why it doesn't fail on the first call, just the second. – GrizzlyManBear Aug 24 '15 at 19:24
  • This makes even less sense. You suggested to include the check `custnameTF.getText() != null`, so you think that `custnameTF.getText()` returns `null`. And then you say that this `custnameTF.getText().equals("")` wouldn't fail ... because `null.equals("")` is possible? And *"but you cannot set a null value to another variable"* is either unclear or wrong. You can set every variable to `null` if it isn't a primitive type. – Tom Aug 24 '15 at 19:36
  • [THIS THREAD](http://stackoverflow.com/questions/2601978/how-to-check-if-my-string-is-equal-to-null) explains procedure to check for null values in strings. You must do this first in order to avoid Null Pointer Exceptions. – GrizzlyManBear Aug 25 '15 at 12:24
  • I know how to check for `null`, but it seems that you don't understand, that `custnameTF.getText()` can't be `null` if the NPE occurs one the line OP mentioned. Maybe sometime you will see, that it is `customerfile` that is `null`. Not meant to be offending. – Tom Aug 25 '15 at 16:54