Array of textboxes - can't access data [SOLVED.. by self]
-
Hello All, I'm using Netbeans 7.01 and I'm trying to validate input in 2 textboxes by running a validation loop. So, I decided to create an Array of textboxes, using existing textboxes already placed on the form. When I try to access a value in the Array, a nullpointerexception is thrown. Here is part of my code:
// This is an event that is supposed to clear the contents
// of a third jTextField, if the first input box
// has a key typed in it:private void jTextField1KeyTyped(java.awt.event.KeyEvent evt)
{
// Clear previous result in jTextField3,
// if jTextField1 text is changed:arrJTxtFld[2].setText(""); // This is where the nullpointerexception occurs
}// This is the code for the JTextField array:
private JTextField[] arrJTxtFld = {jTextField1, jTextField2, jTextField3};
-
Hello All, I'm using Netbeans 7.01 and I'm trying to validate input in 2 textboxes by running a validation loop. So, I decided to create an Array of textboxes, using existing textboxes already placed on the form. When I try to access a value in the Array, a nullpointerexception is thrown. Here is part of my code:
// This is an event that is supposed to clear the contents
// of a third jTextField, if the first input box
// has a key typed in it:private void jTextField1KeyTyped(java.awt.event.KeyEvent evt)
{
// Clear previous result in jTextField3,
// if jTextField1 text is changed:arrJTxtFld[2].setText(""); // This is where the nullpointerexception occurs
}// This is the code for the JTextField array:
private JTextField[] arrJTxtFld = {jTextField1, jTextField2, jTextField3};
have you initialized the fields jTextField1, jTextField2, jTextField3?
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
have you initialized the fields jTextField1, jTextField2, jTextField3?
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
And then it hit me! A blast from my C# past! All I had to do was just add the following code just under the initComponents(): arrJTxtFld[0] = jTextField1; arrJTxtFld[1] = jTextField2; arrJTxtFld[2] = jTextField3; Now if I could only automate those assignments in a loop! lol A thanks to Naerling for responding earlier this morning with the same solution. I'd figured it out right before going to bed last night.
-
And then it hit me! A blast from my C# past! All I had to do was just add the following code just under the initComponents(): arrJTxtFld[0] = jTextField1; arrJTxtFld[1] = jTextField2; arrJTxtFld[2] = jTextField3; Now if I could only automate those assignments in a loop! lol A thanks to Naerling for responding earlier this morning with the same solution. I'd figured it out right before going to bed last night.
You could use the same line as you had befor:
public Class GuiForm extends JFrame {
// define the array as a member variable, uninitialised:
private JTextField[] arrJTxtFld;public GuiForm() { initComponents(); // now you can use the UI components this.arrJTxtFld = {jTextField1, jTextField2, jTextField3}; }
}
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
You could use the same line as you had befor:
public Class GuiForm extends JFrame {
// define the array as a member variable, uninitialised:
private JTextField[] arrJTxtFld;public GuiForm() { initComponents(); // now you can use the UI components this.arrJTxtFld = {jTextField1, jTextField2, jTextField3}; }
}
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
Hmm for some reason it says not a statement?
public class frmSubnet_Calculator extends javax.swing.JFrame {
private JTextField\[\] arrJTxtFldIntegerOctets; /\*\* Creates new form frmSubnet\_Calculator \*/ public frmSubnet\_Calculator() { initComponents(); // Center program on screen: setLocationRelativeTo(null); arrJTxtFldIntegerOctets = {jTxtFldIntegerOctet1, // error message starts here jTxtFldIntegerOctet2, jTxtFldIntegerOctet3, jTxtFldIntegerOctet4, jTxtFldIntegerOctet5, jTxtFldIntegerOctet6, jTxtFldIntegerOctet7, jTxtFldIntegerOctet8 };
Nagy Vilmos wrote:
You could use the same line as you had befor:
public Class GuiForm extends JFrame {
// define the array as a member variable, uninitialised:
private JTextField[] arrJTxtFld;
public GuiForm() {
initComponents();
// now you can use the UI components
this.arrJTxtFld = {jTextField1, jTextField2, jTextField3};
}
}