ActionListener不起作用 - 它显示NullPointerException

时间:2014-06-05 07:58:44

标签: java swing nullpointerexception

我的ActionListener出了问题。当我启动我的应用程序时,会出现NullPointerException

这是代码:

public class SimulationPopUp extends javax.swing.JFrame {

    private String simulation;

    private Data d;
    /**
     * Creates new form SimulationPopUp
     */
    public SimulationPopUp(String simulation, Data d) throws SQLException, IOException {
        this.simulation=simulation;
        this.d = d;
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        initComponents();
        simulationChosen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                changeSimulation(e);
            }
        });
        simulationChosen.removeAllItems();
        try {
            Object[] o = d.ExtractAllSimulation(simulation);
            for (int i=0; i<o.length-1; i++){
                simulationChosen.addItem(o[i]);
            }
            Object[] param = d.ExtractAllParameters((int)o[o.length-1]);
            jLabel25.setText((String)param[0]);
            jLabel26.setText(""+(Double)param[1]);
            jLabel27.setText(""+(Integer)param[2]);
            jLabel28.setText(""+(Integer)param[3]);
            jLabel29.setText(""+(Double)param[4]);
            jLabel30.setText(""+(Integer)param[5]);
            jLabel31.setText(""+(Integer)param[6]);
            jLabel32.setText(""+(Integer)param[7]);
            jLabel33.setText(""+(Integer)param[8]);
            jLabel34.setText(""+(Integer)param[9]);
            jLabel35.setText(""+(Double)param[10]);
            jLabel36.setText(""+(Integer)param[11]);
            jLabel37.setText(""+(Double)param[12]);
            jLabel38.setText(""+(Integer)param[13]);
            jLabel39.setText(""+(Integer)param[14]);
            jLabel40.setText(""+(Integer)param[15]);
            jLabel41.setText(""+(Integer)param[16]);
            jLabel42.setText(""+(Double)param[17]);
            jLabel43.setText(""+(Integer)param[18]);
            jLabel44.setText(""+(Integer)param[19]);
            jLabel45.setText(""+(Integer)param[20]);
            jLabel46.setText(""+(Integer)param[21]);
        }catch(SQLException e) {
            Logger.getLogger(SimulatorGui.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    private void changeSimulation(java.awt.event.ActionEvent e) {
        String run = simulationChosen.getSelectedItem().toString();
        System.out.println(run);
        ImageIcon image = new ImageIcon("../charts/"+simulation+"/"+run+"/chart.png");
        JLabel label = new JLabel(image);
        label.setSize(imagePanel.getWidth(), imagePanel.getHeight());
        imagePanel.add(label);
        imagePanel.repaint();
    }
}
你能帮帮我吗?

2 个答案:

答案 0 :(得分:0)

默认情况下,在simulationChosen上未选择任何项目。在initComponents中或在创建ActionListener之前选择一个项目。

simulationChosen.setSelectedIndex(0)

确保simulationChosen有要在里面选择的项目。

答案 1 :(得分:0)

添加一个ActionListener,调用changeSimulation(),此方法中的第一个字符串为simulationChosen.getSelectedItem()

但是在你在下一行添加一个ActionListener后,你就调用了simulationChosen.removeAllItems();

当事件被触发并且simulationChosen没有元素时会发生什么?这是你的NullPointer。