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?
Add
players[i] = new Player();
Just before using players[i]