Results 1 to 2 of 2
Thread: JButton setColor problems
- 10-29-2010, 02:20 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 1
- Rep Power
- 0
JButton setColor problems
On click, the background of the button is supposed to cycle between 6 different colors. Not sure what is going wrong here. It will compile, but give "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" on click.
Java Code:private class ColorButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (colorCounter==0) { button1.setBackground(Color.ORANGE); colorCounter++; } else if (colorCounter==1) { button1.setBackground(Color.YELLOW); colorCounter++; } else if (colorCounter==2) { button1.setBackground(Color.CYAN); colorCounter++; } else if (colorCounter==3) { button1.setBackground(Color.LIGHT_GRAY); colorCounter++; } else if (colorCounter==4) { button1.setBackground(Color.GRAY); colorCounter++; } else if (colorCounter==5) { button1.setBackground(Color.BLACK); colorCounter=0; } }
-
You never tell us which line causes the exception, but I'm guessing it's one of the lines where you try to set the color of button1, and that button1 is in fact null. You can check this out by having
Java Code:if (colorCounter==0) { System.out.println("button1 is null: " + (button1 == null)); button1.setBackground(Color.ORANGE); colorCounter++; }
And then if it is in fact null, go back and try to see where you think that you are initializing this JButton.
Similar Threads
-
JTextField setColor()
By Insomniac Riot in forum AWT / SwingReplies: 2Last Post: 10-22-2010, 08:53 PM -
setColor() gives a null error on execution
By gothrog in forum AWT / SwingReplies: 10Last Post: 09-01-2010, 02:06 AM -
Persistence causing problems with JButton 2D Array
By easyp in forum New To JavaReplies: 2Last Post: 04-21-2010, 06:34 PM -
Problems with ActionEvent for JButton
By TrueBear in forum AWT / SwingReplies: 1Last Post: 08-24-2009, 04:26 PM -
Problems with jButton ActionPerformed
By susan in forum AWT / SwingReplies: 3Last Post: 08-07-2007, 04:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks