Results 1 to 20 of 21
Thread: Creating array of JButtons
- 04-15-2011, 11:55 PM #1
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:
I am accessing the array properly using a for loop from index 0 - 8.Java Code:private JButton button_array [] = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9};
Any help will be much appreciated! If you need to see more of the code just ask.
- 04-16-2011, 12:01 AM #2
Are you sure the NullPointerException is saying this is the offending line?
- 04-16-2011, 02:21 AM #3
No the offended lines are:
Basically any attempt to access the array brings up a NullPointerException.Java Code:for (int i = 0; i <= 8; i++) { button_array[i].setEnabled(true); button_array[i].setText(""); }Last edited by tabchas; 04-16-2011 at 02:33 AM.
- 04-16-2011, 02:34 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-16-2011, 02:44 AM #5
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 02:54 AM.
- 04-16-2011, 03:09 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-16-2011, 03:11 AM #7
- 04-16-2011, 03:15 AM #8
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.
- 04-16-2011, 03:17 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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)
This worked as expected and allows me to add all the buttons to the frame without problems.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); } }Last edited by sunde887; 04-16-2011 at 03:20 AM.
- 04-16-2011, 03:23 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-16-2011, 03:27 AM #11
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 03:32 AM.
- 04-16-2011, 03:31 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Would you mind showing us some code of what the button declarations look like? Also, read what pbrock said.
- 04-16-2011, 04:15 AM #13
You can see the complete code here: Here
BTW: I am still getting used to GitHub.
- 04-16-2011, 04:24 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 04-16-2011, 04:43 AM #15
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.
- 04-16-2011, 04:53 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.)
- 04-16-2011, 05:29 AM #17
I tried it didnt work.
I putat the bottomJava Code:private JButton [] button_array;
andin the initialize method. It picks up an illegal statement error.Java Code:button_array [] = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9};
- 04-16-2011, 05:32 AM #18
You have to declare a new array:
Java Code://in the method button_array = new JButton[] {Button1, Button2.....};
- 04-16-2011, 05:35 AM #19
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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};
- 04-16-2011, 05:36 AM #20
Similar Threads
-
Creating Jbuttons, etc. dynamically
By JohnST in forum New To JavaReplies: 2Last Post: 01-30-2010, 04:08 PM -
Creating an array of objects
By geowizard in forum New To JavaReplies: 5Last Post: 11-16-2009, 01:25 AM -
2D Array of JButtons
By stevemcc in forum AWT / SwingReplies: 1Last Post: 02-16-2008, 11:42 PM -
Creating Array of LinkedList
By sasikumardr in forum New To JavaReplies: 1Last Post: 12-11-2007, 10:25 AM -
creating array at runtime
By javaplus in forum New To JavaReplies: 4Last Post: 11-08-2007, 10:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks