从 JTextField 检索文本并将其存储在另一个类的对象中
我目前正在开发一个基于 GUI 的应用程序,并使用 netbeans 7.1 的代码生成功能以及我自己的一些自定义代码,以使我的生活更轻松。
我拥有的是一个 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() 数据,但无法将其存储在数据成员players[]中。
有人知道这件事吗?
I'm currently working on a GUI based application and am using the code-generation feature of netbeans 7.1 along with some custom code of my own to make life easier for me.
What I have is an array of JTextboxes from which I need to retrieve the Text and store in the respective object from an array of objects of a different class.
The problem is that I'm receiving several runtime exceptions after submitting the data.
Here's some relevant snippets of my code:
My custom code for component creation :
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);
}
I have 3 public data members
public int plnum;
public int size;
public Player [] players;
Now here's the code for the submit button that triggers the exceptions:
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();
}
And here's the huge list of exceptions :
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)
I can't figure out what's wrong with my code. Did some trial and error and the only thing I came up with is that if I store the getText() data in a local variable in the button click event method, then there's no error.
I can basically print the getText() data but can't store it in the data member players[].
Anyone know something about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加
players[i] = new Player();
在使用
players[i]
之前Add
players[i] = new Player();
Just before using
players[i]
看来您从未初始化过玩家数组。尝试:
It seems that you never initialized your players array. Try: