重构JTextFields

时间:2014-04-02 20:10:20

标签: java

我在JTabbedPane上重构了很多字段以减少冗余。但是,当我为字段数设置一个常量大小时,我得到一个空指针异常。我不确定为什么会这样。我做错了什么,更重要的是有人可以解释发生了什么?

public class JDBC_GUI {

    private JFrame frame;

    private static final int WINNER_SIZE = 4;
    private JTextField[] winner_fields = new JTextField[WINNER_SIZE];


    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

        JPanel winners_tab = new JPanel();
        tabbedPane.addTab("Winners", null, winners_tab, null);
        winners_tab.setLayout(null);

        for(int i = 0; i < WINNER_SIZE; i++) {
            winner_fields[i].setBounds(212, 30 + (i * 40), 134, 28);
            winners_tab.add(winner_fields[i]);
            winner_fields[i].setColumns(10);
        }
    }
}

异常消息:

java.lang.NullPointerException
at JDBC_GUI.initialize(JDBC_GUI.java:60)
at JDBC_GUI.<init>(JDBC_GUI.java:41)
at JDBC_GUI$1.run(JDBC_GUI.java:28)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

2 个答案:

答案 0 :(得分:1)

此行执行时:

 winner_fields[i].setBounds(212, 30 + (i * 40), 134, 28);

您还没有在winner_fields数组中添加任何内容。所有元素都初始化为其类型的默认值,在本例中为null。因此winner_fields[i]将返回null,当您尝试在其上调用setBounds()时会产生NPE。

解决方案是在使用之前初始化字段:

    for(int i = 0; i < WINNER_SIZE; i++) {
        winner_fields[i] = new JTextField();
        winner_fields[i].setBounds(212, 30 + (i * 40), 134, 28);
        winners_tab.add(winner_fields[i]);
        winner_fields[i].setColumns(10);
    }

答案 1 :(得分:1)

看起来你没有在winner_fields数组中放任何东西,所以在这里引用它会抛出一个空指针:

    for(int i = 0; i < WINNER_SIZE; i++) {
        winner_fields[i].setBounds(212, 30 + (i * 40), 134, 28);
        winners_tab.add(winner_fields[i]);
        winner_fields[i].setColumns(10);
    }

可能的解决办法可能是:

    for(int i = 0; i < WINNER_SIZE; i++) {
        winner_fields[i] = new JTextField();
        winner_fields[i].setBounds(212, 30 + (i * 40), 134, 28);
        winners_tab.add(winner_fields[i]);
        winner_fields[i].setColumns(10);
    }