我目前正在开发一个基于GUI的应用程序,并使用netbeans 7.1的代码生成功能以及我自己的一些自定义代码来使我的生活更轻松。从JTextField中检索文本并存储在另一个类的对象中
我所拥有的是一个JTextbox数组,我需要从中检索文本并将其存储在不同类的对象数组中的相应对象中。
问题是我在提交数据后收到几个运行时异常。
这里是我的代码一些相关片段:
的组件创建我的自定义代码:
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()数据,但无法将其存储在数据成员玩家[]中。
有人知道这件事吗?
那里的东西就行SnakesnLadders.java没有146之前添加
players[i] = new Player();
? – 2012-02-02 07:30:50
line 146:players [i] .setName(plntext [i] .getText()); – 2012-02-02 07:37:39
您的球员[i]为空或您的plntext [i]为空。尝试在控制台上打印它们。 – 2012-02-02 07:39:34