当我尝试设置图像图标时,为什么会出现此错误

时间:2015-03-19 06:22:19

标签: java action actionlistener jcombobox imageicon

我似乎无法使actionPerformed方法正常工作。任何帮助表示感谢,谢谢 这里是AwardingBody类

public abstract class AwardingBody {
    protected String message = "Congratulations you are a winner";

    public String getMessage(){
        return message;
    }

public abstract Prize getPrize(int prize);  

}


这是一个奖品亚型

public class GoldMedal extends Medal{

    @Override
    public ImageIcon getIcon() {
        return new ImageIcon("goldMedal.jpg");
    }

}

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;


public class AwardsGUI extends JFrame implements ActionListener{
    AwardingBody award;
    JPanel panel, comboPanel;
    JTextField text;
    JLabel label;
    JComboBox<String> placeBox, awardingBodyBox;
    String [] places = {"Choose place", "First", "Second", "Third"};
    String [] awardingBodies = {"Select", "Athletics", "HorseShow"};

    public AwardsGUI(){
        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        label = new JLabel();
        text = new JTextField(20);
        comboPanel = new JPanel();
        comboPanel.setLayout(new GridLayout(1,2));
        placeBox = new JComboBox<String>(places);
        placeBox.addActionListener(this);
        awardingBodyBox = new JComboBox<String>(awardingBodies);
        awardingBodyBox.addActionListener(this);
        comboPanel.add(placeBox);
        comboPanel.add(awardingBodyBox);

        panel.add(text, BorderLayout.NORTH);
        panel.add(label, BorderLayout.CENTER);
        panel.add(comboPanel, BorderLayout.SOUTH);

        this.add(panel);
        setSize(300,300);
        setVisible(true);
    }

    public static void main(String [] args){
        new AwardsGUI();
    }

    public void actionPerformed(ActionEvent e) {
        if(placeBox.getSelectedItem().equals("First") && awardingBodyBox.getSelectedItem().equals("Athletics")){
            award = new AthleticsAwardingBody();
            text.setText(award.getMessage());
            label.setIcon(award.getPrize(1).getIcon());
        }
        else if(placeBox.getSelectedItem().equals("Second") && awardingBodyBox.getSelectedItem().equals("Athletics")){
            award = new AthleticsAwardingBody();
            text.setText(award.getMessage());
            label.setIcon(award.getPrize(2).getIcon());
        }
        else if(placeBox.getSelectedItem().equals("Third") && awardingBodyBox.getSelectedItem().equals("Athletics")){
            award = new AthleticsAwardingBody();
            text.setText(award.getMessage());
            label.setIcon(award.getPrize(3).getIcon());
        }
        else if(placeBox.getSelectedItem().equals("First") && awardingBodyBox.getSelectedItem().equals("HorseShow")){
            award = new HorseShowAwardingBody();
            text.setText(award.getMessage());
            label.setIcon(award.getPrize(1).getIcon());
        }
        else if(placeBox.getSelectedItem().equals("Second") && awardingBodyBox.getSelectedItem().equals("HorseShow")){
            award = new HorseShowAwardingBody();
            text.setText(award.getMessage());
            label.setIcon(award.getPrize(2).getIcon());
        }
        else if(placeBox.getSelectedItem().equals("Third") && awardingBodyBox.getSelectedItem().equals("HorseShow")){
            award = new HorseShowAwardingBody();
            text.setText(award.getMessage());
            label.setIcon(award.getPrize(3).getIcon());
        }
    }




}

和例外是

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at AwardsGUI.actionPerformed(AwardsGUI.java:50)
    at javax.swing.JComboBox.fireActionEvent(Unknown Source)
    at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

0 个答案:

没有答案