Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Array of textboxes - can't access data [SOLVED.. by self]

Array of textboxes - can't access data [SOLVED.. by self]

Scheduled Pinned Locked Moved Java
javadata-structures
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LEKnowlton
    wrote on last edited by
    #1

    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};

    N 1 Reply Last reply
    0
    • L LEKnowlton

      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};

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • N Nagy Vilmos

        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

        L Offline
        L Offline
        LEKnowlton
        wrote on last edited by
        #3

        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.

        N 1 Reply Last reply
        0
        • L LEKnowlton

          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.

          N Offline
          N Offline
          Nagy Vilmos
          wrote on last edited by
          #4

          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

          L 1 Reply Last reply
          0
          • N Nagy Vilmos

            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

            L Offline
            L Offline
            LEKnowlton
            wrote on last edited by
            #5

            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};
            }
            }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups