Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default Creating array of JButtons

    Hey

    I am using the Netbeans IDE and created 9 JButtons using their GUI Builder. I want to store these buttons into one array. I don't know if Netbeans is doing something to prevent me to not do this but a NullPointerException pulls up.

    This is the code that I use to store my 9 buttons:

    Java Code:
    private JButton button_array [] = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9};
    I am accessing the array properly using a for loop from index 0 - 8.

    Any help will be much appreciated! If you need to see more of the code just ask.

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Are you sure the NullPointerException is saying this is the offending line?

  3. #3
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default

    No the offended lines are:

    Java Code:
    for (int i = 0; i <= 8; i++)
        {
                button_array[i].setEnabled(true);
                button_array[i].setText("");
        }
    Basically any attempt to access the array brings up a NullPointerException.
    Last edited by tabchas; 04-16-2011 at 03:33 AM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    A NullPointerException has nothing to do with your IDE. It occurs at runtime when you try and do something with a variable which is null.

    There are one of two things here which could be null: the array button_array and the array element button_array[i]. First use SYstem.out.println() to figure out what is null in your case:

    Java Code:
    System.out.println("button_array=" + button_array);
    for (int i = 0; i <= 8; i++)
      {
        System.out.println("i=" + i);
        System.out.println("button_array[i]=" + button_array[i]);
        button_array[i].setEnabled(true);
        button_array[i].setText("");
      }

    Once you have figured out what is null go back through your code to see where you thought you had given that thing a nonnull value ... and figure out why that did not happen.

  5. #5
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default

    The reason I said that the IDE might affect it is that I am not declaring the buttons, the GUI Builder does it automatically. But I also do not think this would affect it.

    The first line prints: "button_array=[Ljavax.swing.JButton;@7455d93d". The next print statements brings up the first element. It reports that it is equal to "null". However, since I did not declare or initialize the buttons, I do not know what to do. I am pretty sure though that all of the buttons are null.

    All I did was declare the array here:
    Java Code:
    private JButton button_array [] = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9};

    Could it be possible that you cannot group these pre-initialized objects into an array?

    Thanks!
    Last edited by tabchas; 04-16-2011 at 03:54 AM.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't know what your code looks like, however; an easy way to test whether it matters if the buttons are initialized before being stored in the array is to move that array initializer to after all the buttons are initialized. If this solves your problem you will now know which order is correct. If it doesn't change things then you will have to look more into the code and see what's going wrong.

  7. #7
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    No it's just you need to explicitly initialize them all.

  8. #8
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default

    ra4king: What do you mean I have to explicitly initialize them all? Aren't they initialized already in the Generated Code by the IDE?

    sunde887: No I just tested it after all the buttons are being initialized. The errors still pull up.

    I still do not know what to do. I tried looking online and I looked into the code as well. If possible, can one of you guys try to simulate the same situation and see if it works for you guys? That would allow us to compare and target the problem area.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How are the buttons declared in the code? Are they all initialized?

    I made this little snippet to show how it should look(of course this is a small example)
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class ButtonArray{
    	public static void main(String[] args){
    		JFrame frame = new JFrame("HELLO");
    		JButton x = new JButton("X");
    		JButton y = new JButton("Y");
    		JButton z = new JButton("Z");
    		frame.setLayout(new FlowLayout());
    		
    		JButton[] buttons = {x, y, z};
    		
    		for(int i = 0; i < buttons.length; i++){
    			frame.add(buttons[i]);
    		}
    		frame.setSize(100, 500);
    		frame.setVisible(true);
    	}
    }
    This worked as expected and allows me to add all the buttons to the frame without problems.
    Last edited by sunde887; 04-16-2011 at 04:20 AM.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The problem is that the variable Button1 (it would be much better as button1) is declared somewhere. And it has the value null when it is declared. That value (null) is being put into an array and so you get the NPE when you subsequently dereference the array element by calling the methods on it.

    To avoid the NPE you must add nonnull values to the array. In other words the values must be added to the array after the variables have been initialised.

  11. #11
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default

    Yes it is initialized the same way. Try it in Netbeans with the GUI Builder to see if it makes a difference and also to simulate the same situation.

    pbrockway2: They are being initialized in the Generated code as such:

    Button1 = new javax.swing.JButton();
    Button1.setFont(new java.awt.Font("Tahoma", 1, 18));
    Button1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    Button1ActionPerformed(evt);
    }
    });

    Is initializing the same as creating a new object like in the first line of code in the snippet above?


    Thanks!
    Last edited by tabchas; 04-16-2011 at 04:32 AM.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Would you mind showing us some code of what the button declarations look like? Also, read what pbrock said.

  13. #13
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default

    You can see the complete code here: Here

    BTW: I am still getting used to GitHub.

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    In that code button_array is populated using the variables Button1 etc(near the end of the file) before those variables have been given nonnull values. Perhaps you should populate the array in initComponents() after you have given the variables nonnull values.

  15. #15
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default

    Also, I cannot edit the initComponents() method. Netbeans does not allow you.

    Wow... this is really weird. If I build the array in my initialize method, it works but it is now a local variable so I can only access it from inside one method. I need to make it an instance variable.

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I didn't notice that you had another variable with the same name in initialize(). Possibly declare it as an instance variable and initialise it in the initialize() method.

    (At the moment the button_array you declare in initialize() is hiding the instance variable of the same name.)

  17. #17
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default

    I tried it didnt work.

    I put
    Java Code:
    private JButton [] button_array;
    at the bottom

    and
    Java Code:
    button_array [] = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9};
    in the initialize method. It picks up an illegal statement error.

  18. #18
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You have to declare a new array:
    Java Code:
    //in the method
    button_array = new JButton[] {Button1, Button2.....};

  19. #19
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
        // declare the array (at bottom)
    private JButton [] button_array;
    // ...
    JButton Button1 = ...
    // etc
        // initialise and populate the array with nonnull values
    button_array = new JButton[]{Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9};

  20. #20
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

Page 1 of 2 12 LastLast

Similar Threads

  1. Creating Jbuttons, etc. dynamically
    By JohnST in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 05:08 PM
  2. Creating an array of objects
    By geowizard in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 02:25 AM
  3. 2D Array of JButtons
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 12:42 AM
  4. Creating Array of LinkedList
    By sasikumardr in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 11:25 AM
  5. creating array at runtime
    By javaplus in forum New To Java
    Replies: 4
    Last Post: 11-08-2007, 11:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •