Java JList - 参数不能为null

时间:2015-05-31 19:02:00

标签: java pointers null jlist

编译Java程序时遇到问题。我不知道为什么在第103行创建新对象时出现此错误...我得到的错误是:

    Exception in thread "main" java.lang.NullPointerException: Argument must not be null
    at javax.swing.Spring.checkArg(Spring.java:688)
    at javax.swing.Spring.width(Spring.java:657)
    at javax.swing.SpringLayout.applyDefaults(SpringLayout.java:1093)
    at javax.swing.SpringLayout.putConstraints(SpringLayout.java:1123)
    at javax.swing.SpringLayout.getConstraints(SpringLayout.java:1158)
    at javax.swing.SpringLayout.putConstraint(SpringLayout.java:1080)
    at javax.swing.SpringLayout.putConstraint(SpringLayout.java:1075)
    at javax.swing.SpringLayout.putConstraint(SpringLayout.java:1041)
    at cwiczenie9.Cwiczenie9.setSpringLayoutConstraint(Cwiczenie9.java:204)
    at cwiczenie9.Cwiczenie9.<init>(Cwiczenie9.java:31)
    at cwiczenie9.Cwiczenie9.main(Cwiczenie9.java:232)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

我的应用程序代码在这里:

package cwiczenie9;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Cwiczenie9 extends JFrame {

    private Container container;
    private SpringLayout sprLay;
    private JLabel[] jLabel;
    private JTextField[] jTextField;
    private JRadioButton[] jRadioButton;
    private ButtonGroup buttonGroup;
    private SpinnerModel spinnerModel;
    private JSpinner jSpinner;
    private JButton[] jButton;
    private JList jList;
    private final String[] jListItems = {"Pies", "Kot", "Chomik", "Królik", 
        "Jaszczurka", "Pająk", "Rybki", "Owady"};

    public Cwiczenie9() {
        addContainer();
        setFrameProperties();
        createJLabel(5);
        createJTextField(2);
        createJRadioButton(2);
        createJSpinner(0, 0, 99, 1);
        createJButton(2);
        setSpringLayoutConstraint();
    }

    private void addContainer() {
        sprLay = new SpringLayout();
        container = this.getContentPane();
        container.setLayout(sprLay);    
    }

    private void setFrameProperties() {
        this.setTitle("Ankieta");           
        this.setSize(300, 300);
        this.setLocation(150, 150);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void createJLabel(int count) {
        jLabel = new JLabel[count];
        for(int i = 0; i < count; i++)
            jLabel[i] = new JLabel();
        jLabel[0].setText("Imię:");
        jLabel[1].setText("Nazwisko:");
        jLabel[2].setText("Płeć:");
        jLabel[3].setText("Wiek:");
        jLabel[4].setText("Wybierz zwierzęta, które posiadasz w domu:");
        for(JLabel jl : jLabel)
            container.add(jl);
    }

    private void createJTextField(int count) {
        jTextField = new JTextField[count];
        for(int i = 0; i < count; i++)
            jTextField[i] = new JTextField();
        jTextField[0].setColumns(15);
        jTextField[1].setColumns(15);
        for(JTextField jtf : jTextField)
            container.add(jtf);
    }

    private void createJRadioButton(int count) {
        jRadioButton = new JRadioButton[count];
        buttonGroup = new ButtonGroup();
        for(int i = 0; i < count; i++)
            jRadioButton[i] = new JRadioButton();
        jRadioButton[0].setText("Kobieta");
        jRadioButton[1].setText("Mężczyzna");
        for(JRadioButton jrb : jRadioButton) {
            buttonGroup.add(jrb);
            container.add(jrb);
        }
    }

    private void createJSpinner(int initialValue, int minValue,
            int maxValue, int step) {
        spinnerModel = new SpinnerNumberModel(initialValue, 
                minValue, 
                maxValue, 
                step);
        jSpinner = new JSpinner(spinnerModel);
        container.add(jSpinner);
    }

    private void createJButton(int count) {
        jButton = new JButton[count];
        for(int i = 0; i < count; i++)
            jButton[i] = new JButton();
        jButton[0].setText("Zatwierdź");
        jButton[1].setText("Anuluj");
        for(JButton jb : jButton)
            container.add(jb);
    }

    private void createJList() {
        jList = new JList(jListItems);
        jList.setPreferredSize(new Dimension(120, 80));
        jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jList.setLayoutOrientation(JList.VERTICAL_WRAP);
        jList.setVisibleRowCount(5);
        container.add(jList);
    }

    private void setSpringLayoutConstraint() {
        // jLabel[0] - NORTH <-> NORTH
        sprLay.putConstraint(SpringLayout.NORTH, jLabel[0],
                10,
                SpringLayout.NORTH, container);
        // jLabel[0] - WEST <--> WEST
        sprLay.putConstraint(SpringLayout.WEST, jLabel[0],
                10,
                SpringLayout.WEST, container); 

        // jTextField[0] - NORTH <-> NORTH
        sprLay.putConstraint(SpringLayout.NORTH, jTextField[0],
                10,
                SpringLayout.NORTH, container);
        // jTextField[0] - WEST <-> EAST
        sprLay.putConstraint(SpringLayout.WEST, jTextField[0],
                10,
                SpringLayout.EAST, jLabel[0]);

        // jLabel[1] - NORTH <-> SOUTH
        sprLay.putConstraint(SpringLayout.NORTH, jLabel[1],
                15,
                SpringLayout.SOUTH, jLabel[0]);
        // jLabel[1] - WEST <--> WEST
        sprLay.putConstraint(SpringLayout.WEST, jLabel[1],
                10,
                SpringLayout.WEST, container); 

        // jTextField[1] - NORTH <-> SOUTH
        sprLay.putConstraint(SpringLayout.NORTH, jTextField[1],
                15,
                SpringLayout.SOUTH, jTextField[0]);
        // jTextField[1] - WEST <-> EAST
        sprLay.putConstraint(SpringLayout.WEST, jTextField[1],
                10,
                SpringLayout.EAST, jLabel[1]);

        // jLabel[2] - NORTH <-> SOUTH
        sprLay.putConstraint(SpringLayout.NORTH, jLabel[2],
                15,
                SpringLayout.SOUTH, jLabel[1]);
        // jLabel[2] - WEST <--> EAST
        sprLay.putConstraint(SpringLayout.WEST, jLabel[2],
                10,
                SpringLayout.WEST, container);

        // jRadioButton[0] - NORTH <-> SOUTH
        sprLay.putConstraint(SpringLayout.NORTH, jRadioButton[0],
                15,
                SpringLayout.SOUTH, jTextField[1]);
        // jRadioButton[0] - WEST <-> EAST
        sprLay.putConstraint(SpringLayout.WEST, jRadioButton[0],
                10,
                SpringLayout.EAST, jLabel[2]);

        // jRadioButton[1] - NORTH <-> SOUTH
        sprLay.putConstraint(SpringLayout.NORTH, jRadioButton[1],
                15,
                SpringLayout.SOUTH, jTextField[1]);
        // jRadioButton[1] - WEST <-> EAST
        sprLay.putConstraint(SpringLayout.WEST, jRadioButton[1],
                10,
                SpringLayout.EAST, jRadioButton[0]);

        // jLabel[3] - NORTH <-> SOUTH
        sprLay.putConstraint(SpringLayout.NORTH, jLabel[3],
                15,
                SpringLayout.SOUTH, jLabel[2]);
        // jLabel[3] - WEST <-> WEST
        sprLay.putConstraint(SpringLayout.WEST, jLabel[3],
                10,
                SpringLayout.WEST, container);

        // jSpinner - NORTH <-> SOUTH
        sprLay.putConstraint(SpringLayout.NORTH, jSpinner,
                15,
                SpringLayout.SOUTH, jRadioButton[0]);
        // jSPinner - WEST <-> EAST
        sprLay.putConstraint(SpringLayout.WEST, jSpinner,
                10,
                SpringLayout.EAST, jLabel[3]);

        // jLabel[4] - NORTH <-> SOUTH
        sprLay.putConstraint(SpringLayout.NORTH, jLabel[4],
                15,
                SpringLayout.SOUTH, jLabel[3]);
        // jLabel[4] - WEST <-> WEST
        sprLay.putConstraint(SpringLayout.WEST, jLabel[4],
                10,
                SpringLayout.WEST, container); 

        // jList - NORTH <-> SOUTH
        sprLay.putConstraint(SpringLayout.NORTH, jList,
                15,
                SpringLayout.SOUTH, jLabel[4]);
        // jList - WEST <-> WEST
        sprLay.putConstraint(SpringLayout.WEST, jList,
                10,
                SpringLayout.WEST, container);

        // jButton[0] - NORTH <-> SOUTH
        sprLay.putConstraint(SpringLayout.NORTH, jButton[0],
                15,
                SpringLayout.SOUTH, jList);
        // jButton[0] - WEST <-> WEST
        sprLay.putConstraint(SpringLayout.WEST, jButton[0],
                10,
                SpringLayout.WEST, container);

        // jButton[1] - NORTH <-> SOUTH
        sprLay.putConstraint(SpringLayout.NORTH, jButton[1],
                15,
                SpringLayout.SOUTH, jList);
        // jButton[1] - WEST <-> EAST
        sprLay.putConstraint(SpringLayout.WEST, jButton[1],
                10,
                SpringLayout.EAST, jButton[0]);
    }

    public static void main(String[] args) {
        Cwiczenie9 cw9 = new Cwiczenie9();
        cw9.setVisible(true);
    } 
}

提前感谢您的帮助:)

2 个答案:

答案 0 :(得分:0)

您正在尝试访问第204行上方法null中似乎为setSpringLayoutConstraint()的对象。需要更多信息来回答这个问题。

向我们展示方法setSpringLayoutConstraint()的代码,并为更详细的答案编号。

答案 1 :(得分:0)

我发现了问题。我没有在构造函数中调用createJList()函数。一切都运作良好。