【问题标题】:Java Classes Extend and InheritanceJava 类的扩展和继承
【发布时间】: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


【解决方案1】:

我不确定我是否清楚地了解你。但是尝试这样的事情:

Booking customerbooking = new Booking();
Customer cust = customerbooking.getCustomer();
cust.setPersonName((custnameTF.getText()));
cust.setPersonSurname((custsurnameTF.getText()));

您不能从Booking 调用Customer 的方法。首先,您需要获取对您的 Customer 对象的引用。

你不能这样做:

customerbooking.setPersonName((custnameTF.getText()));
customerbooking.setPersonSurname((custsurnameTF.getText()));

【讨论】:

  • 我添加了客户部分,但现在尝试保存预订文件时出现异常。线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException
  • 您可以将当前代码保存到 Gist 并提供链接吗?或者只是给出新的代码块和完整的异常输出。为什么你的问题中有两个相等的代码块?
  • 现在添加您的异常输出。
  • 抱歉回复晚了。在顶部添加了异常。谢谢
  • 你的班级在BookingFrame.java吗? 357线是什么?
【解决方案2】:

你的导师想要的可能是这样的。如果不是,请更具体地说明问题。

class Customer {
    private int someVal = 42;
   // lots of instance variables and methods
   public int getSomeVal() {
      return someVal;
   }
}

class Booking {
  Customer c;
  int fortytwo;
  // more variables
  public Booking() {}
  public Booking(Customer c) {
    this.c = c;
    fortytwo = c.getSomeVal();
    // more stuff
  }
  // more code
}

主要:

Customer c = new Customer();
Booking b = new Booking(c);

有了 Customer 引用,您现在可以访问 Booking 类中的 Customer 方法,或者您拥有 Customer 对象的任何其他地方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多