从JTextField中检索文本并存储在另一个类的对象中

时间:2012-02-02 07:24:39

标签: java swing jtextfield

我目前正在开发一个基于GUI的应用程序,并且正在使用netbeans 7.1的代码生成功能以及我自己的一些自定义代码,以便让我的生活更轻松。

我所拥有的是一个JTextbox数组,我需要从另一个类的对象数组中检索Text并存储在相应的对象中。

问题是我在提交数据后收到了几个运行时异常。

以下是我的代码的一些相关摘要:

我的组件创建自定义代码:

plnlabel = new javax.swing.JLabel[3];
plntext = new javax.swing.JTextField[3];
int i;
for(i=0;i<3;i++)
{
    plnlabel[i] = new JLabel("Player "+(i+1)+": ");
    plnlabel[i].setVisible(false);
    plntext[i] = new JTextField("Player"+(i+1)+" Name");
    plntext[i].setVisible(false);
}

我有3个公共数据成员

public int plnum;
public int size;
public Player [] players;

现在,这是触发例外的提交按钮的代码:

 private void namesubActionPerformed(java.awt.event.ActionEvent evt)                                        
{                                            
    plnum = nop.getSelectedIndex()+1;
    namesub.setVisible(false);
    customizelabel.setVisible(true);
    plnamelabel.setVisible(false);
    int i;
    for(i=0;i<plnum;i++)
    {
        plnlabel[i].setVisible(false);
        plntext[i].setVisible(false);
        players[i].setName(plntext[i].getText());
    }
    sizeb.setVisible(true);
    sizesel.setVisible(true);
    sizesub.setVisible(true);
    pack();
}

这是一个巨大的例外列表:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at snakesnladders.SnakesnLadders.namesubActionPerformed(SnakesnLadders.java:146)
    at snakesnladders.SnakesnLadders.access$000(SnakesnLadders.java:12)
    at snakesnladders.SnakesnLadders$1.actionPerformed(SnakesnLadders.java:86)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

我无法弄清楚我的代码有什么问题。做了一些试验和错误,我唯一想到的是,如果我在按钮点击事件方法中将getText()数据存储在局部变量中,那么就没有错误。

我基本上可以打印getText()数据,但无法将其存储在数据成员播放器[]中。

有人对此有所了解吗?

2 个答案:

答案 0 :(得分:1)

添加

players[i] = new Player();

在使用players[i]

之前

答案 1 :(得分:-1)

似乎你从未初始化你的球员阵列。尝试:

public Player [] players = new Player[3];