【发布时间】:2015-08-24 09:24:33
【问题描述】:
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)
所以...过去 3 天一直在处理这个问题。解释我需要什么有点困难..但会尽量做到这一点。程序“使用”工作正常。但是我的导师告诉我要更改我的预订课程……我的预订课程正在扩展我的客户课程以获取其他属性,例如姓名、姓氏、年龄等……但是(这是有道理的)我被告知要做我的客户预订等级的财产,而不是从它延伸。由于我不再扩展客户类,因此在尝试获取姓名、姓氏等时,我的代码中出现错误。因为我不再从客户类扩展。如何将客户作为属性包含在内,并且仍然从客户类中获取客户属性。谢谢
private void loadCustomerActionPerformed(java.awt.event.ActionEvent evt) {
Customer customerfile = null;
try {
final JFileChooser chooser = new JFileChooser("Customers/");
int chooserOption = chooser.showOpenDialog(null);
if (chooserOption == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
ObjectInputStream in = new ObjectInputStream(
new FileInputStream(file)
);
customerfile = (Customer) in.readObject();
custnameTF.setText(customerfile.getPersonName());
custsurnameTF.setText(customerfile.getPersonSurname());
custidTF.setText(customerfile.getPersonID());
consnameTF.setText(customerfile.getConsultantname());
conssurnameTF.setText(customerfile.getConsultantsurname());
considTF.setText(customerfile.getConsulid());
in.close();
} else {
throw new CancelException("Canceled Operation");
}
} catch (IOException ex) {
System.out.println("Error Loading File" + ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println("Error Loading Class");
} catch (CancelException ex) {
JOptionPane.showMessageDialog(this, "Canceled Loading Customer",
"Canceled", JOptionPane.INFORMATION_MESSAGE);
}
}
private void savebookingButtonActionPerformed(java.awt.event.ActionEvent evt) {
Booking customerbooking = new Booking();
Customer cust = 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);
cust.setPersonName((custnameTF.getText()));
cust.setPersonSurname((custsurnameTF.getText()));
cust.setPersonID((custidTF.getText()));
cust.setConsultantname(consnameTF.getText());
cust.setConsultantsurname((conssurnameTF.getText()));
cust.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();
}
private void custnameTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void custidTFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void loadbookingButtonActionPerformed(java.awt.event.ActionEvent evt) {
Booking bookingfile = null;
Customer custfile = null;
try {
final JFileChooser chooser = new JFileChooser("Bookings/");
int chooserOption = chooser.showOpenDialog(null);
if (chooserOption == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
ObjectInputStream in = new ObjectInputStream(
new FileInputStream(file)
);
bookingfile = (Booking) in.readObject();
custnameTF.setText(custfile.getPersonName());
custsurnameTF.setText(custfile.getPersonSurname());
custidTF.setText(custfile.getPersonID());
consnameTF.setText(custfile.getConsultantname());
conssurnameTF.setText(custfile.getConsultantsurname());
considTF.setText(custfile.getConsulid());
locationCB.setSelectedItem(bookingfile.getFlightlocation());
timeCB.setSelectedItem(bookingfile.getFlighttime());
feeCB.setSelectedItem(bookingfile.getFlightfee());
carRB.setSelected(bookingfile.getCar());
insuranceRB.setSelected(bookingfile.getInsurance());
in.close();
} else {
throw new CancelException("Canceled Operation");
}
} catch (IOException ex) {
System.out.println("Error Loading File" + ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println("Error Loading Class");
} catch (CancelException ex) {
JOptionPane.showMessageDialog(this, "Canceled Loading Booking",
"Canceled", JOptionPane.INFORMATION_MESSAGE);
}
}
private void createCustbtnActionPerformed(java.awt.event.ActionEvent evt) {
CustomerFrame nc = new CustomerFrame();
nc.setVisible(true);
}
【问题讨论】:
-
代码太多了。只需输入重现问题所需的最少代码并明确说明确切的错误是什么。
标签: java class inheritance properties constructor