我目前正在開發一個基於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