【问题标题】:awt eventqueue 0 nullpointerexception (sample code) [duplicate]awt eventqueue 0 nullpointerexception(示例代码)[重复]
【发布时间】:2013-03-21 00:11:50
【问题描述】:

好的,我收到此错误 awt eventqueue 0 nullpointerexception 错误。当我尝试删除 JPanel 时。

让我感到困惑的是,当我点击价格按钮时,它会删除 JPanel 的时间。它工作得非常好,但是当我点击时间按钮时,它不会删除价格面板,而是我得到一个 awt eventqueue 错误。下面的第一个代码显示了我的主类、时间类和价格类。抱歉,我重新发布了我想发布我的代码。 下面是代码示例

   import javax.swing.JOptionPane;
   import javax.swing.border.TitledBorder;
   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;

   public class events extends JFrame {
   // variables for JPanel


  private JButton timeButton;
  private JButton priceButton;

       setLayout(new BorderLayout()); 

  buttonPanel = new JPanel();
  buttonPanel.setBackground(Color.lightGray);
  buttonPanel.setBorder( new TitledBorder("Main Menu"));


         timeButton = new JButton("Time"); 
  buttonPanel.add(timeButton);

  priceButton = new JButton("Price");
  buttonPanel.add(priceButton);




           buttontime clickTime = new buttontime(); // event created when time button is clicked
  timeButton.addActionListener(clickTime);


         //ActionListener created for price
   buttonprice ClickPrice = new buttonprice(); // event created when price button is clicked
   priceButton.addActionListener(ClickPrice);


         public class buttontime implements ActionListener { //creating actionlistener for clicking on timebutton to bring up a combobox

public void actionPerformed(ActionEvent clickTime) {
    Price priceObject = new Price();
    priceObject.getPricepanel();
    remove(priceObject.getPricepanel());
    priceObject.getPricepanel().revalidate();

    add(timeObject.getTimePanel(), BorderLayout.EAST);
    timeObject.getTimePanel().revalidate();

   }
}

      //This one gives me 0 errors.
           public class buttonprice implements ActionListener { //creating actionlistener for clicking on      timebutton to bring up a combobox

   public void actionPerformed(ActionEvent ClickPrice) {
    Price priceObject = new Price();
    priceObject.SelectPrice();
    remove(timeObject.getTimePanel());
    timeObject.getTimePanel().revalidate();

    add(priceObject.getPricepanel(), BorderLayout.EAST);
    priceObject.getPricepanel().revalidate();

} }

         TIME CLASS

             class Time
 {

   private JComboBox    timeAirportbox;//comboboxes declared

     private String[] airport = {"","East Midlands", "Birmingham", "Manchester", "Heathrow"};//array of    airports declared
    private String[] destination = {"","New York", "Dahab", "Rome", "Sydney", "Tokyo"};//array of destinations declared
 private JPanel timePanel;
 private JLabel airportLabel;  
  private JLabel destinationLabel;
 public void SelectTime() {   

 //combobox objects created
      timePanel = new JPanel();
  timePanel.setBackground(Color.GRAY);

      timePanel.setBorder( new TitledBorder("Time"));
  timeAirportbox = new JComboBox(airport);//array is inserted into the JComboBox
  timePanel.add(timeAirportbox);
  timeAirportbox.setVisible(true);

}
   public JPanel getTimePanel() {
    return timePanel;
    }

     public JComboBox getAirportBox() {
    return timeAirportbox;      
    }


    }

            PRICE CLASS


                class Price {

     private JPanel pricePanel;
      private JLabel tester;

    public void SelectPrice() {
    pricePanel = new JPanel();
  pricePanel.setBackground(Color.YELLOW);
  pricePanel.setPreferredSize(new Dimension(400, 400));
  tester = new JLabel("HIFHI");
  pricePanel.add(tester);

  }

   public JPanel getPricepanel() {
    return pricePanel;
    }
    }

【问题讨论】:

  • 您的示例没有声明或初始化timeObjectpriceObject。如果您要做的只是重新分配它,我看不出将 buttonPanel 传递给构造函数的意义
  • 请不要打开具有完全相同问题的新问题,而是编辑第一个问题以包含相关详细信息:-)

标签: java swing user-interface


【解决方案1】:

你有无穷无尽的问题和问题。

两个直接是...

在你Price类中,你永远不会初始化pricePanel....

class Price {

    private JPanel pricePanel;
    private JLabel tester;

    public void SelectPrice() {
        pricePanel = new JPanel();
        pricePanel.setBackground(Color.YELLOW);

        tester = new JLabel("HIFHI");
        pricePanel.add(tester);

    }

    public JPanel getPricepanel() {
        return pricePanel;
    }
}

这就是导致您的NullPointerException 的原因。

您将面临的第二个问题是您创建了PriceObject 的本地副本,但有些期望能够删除先前实例化对象添加的面板

public class buttontime implements ActionListener { //creating actionlistener for clicking on timebutton to bring up a combobox

    public void actionPerformed(ActionEvent clickTime) {
        Price priceObject = new Price();
        priceObject.getPricepanel();
//----> Remove a local reference of price panel <---- //       
        remove(priceObject.getPricepanel());
        priceObject.getPricepanel().revalidate();

        add(timeObject.getTimePanel(), BorderLayout.EAST);
        timeObject.getTimePanel().revalidate();

    }
}

//This one gives me 0 errors.
public class buttonprice implements ActionListener { //creating actionlistener for clicking on      timebutton to bring up a combobox

    public void actionPerformed(ActionEvent ClickPrice) {
        Price priceObject = new Price();
        priceObject.SelectPrice();
        remove(timeObject.getTimePanel());
        timeObject.getTimePanel().revalidate();

//----> Adding a local reference of price panel <---- //       
        add(priceObject.getPricepanel(), BorderLayout.EAST);
        priceObject.getPricepanel().revalidate();

    }
}

【讨论】:

  • 非常感谢您的帮助,我还没有放弃。本地参考是什么意思?
  • 每个方法都会创建一个Price 的实例,然后尝试添加或删除它的面板。添加没问题,但删除不起作用,因为面板的值与添加的值不同
  • 所以我无法解决它?
  • 当然,不要在方法中创建本地引用。您需要维护对“当前”价格对象的类级别引用。当您需要移除面板时,请使用此参考(如果不是null)。当需要添加新实例时,创建一个新实例并将其分配给类级引用
  • setPreferredSize ?? Boooooohhhhh ...不要向新手展示最坏的做法,最后那些往往是他们唯一记得的东西:-)
猜你喜欢
  • 2013-06-14
  • 2017-08-10
  • 2018-09-26
  • 2017-05-09
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
  • 2013-03-07
  • 1970-01-01
相关资源
最近更新 更多