943,945 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 365
  • Java RSS
Nov 3rd, 2009
0

HELP with code

Expand Post »
I have to write a program that has a tic tac toe board. When you click it, it should go from one image to another. My code compiles fine, but it doesnt run. I get some errors:

Exception in thread "main" java.lang.NullPointerException
at TicTacToe3.<init>(TicTacToe3.java:27)
at TicTacToe3.main(TicTacToe3.java:41)

My code:

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class TicTacToe3 extends JFrame{
  6.  
  7. JButton x;
  8. ImageIcon imageO;
  9. JButton o;
  10. ImageIcon imageX;
  11. JButton[][] button = new JButton[3][3];
  12. int i;
  13. int j;
  14.  
  15. TicTacToe3(){
  16. super("TicTacToe2");
  17. setSize(600, 300);
  18. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. setLocationRelativeTo(null);
  20. setLayout(new GridLayout(3,3));
  21.  
  22. JButton[][] button = new JButton[3][3];
  23. for(int i=0;i<3;i++){
  24. for(int j=0;j<3;j++){
  25. button[i][j].addMouseListener(new ButtonListener());
  26. add(button[i][j]);
  27. }
  28. }
  29.  
  30. ImageIcon imageX = new ImageIcon("kate.jpg");
  31. button[i][j].setIcon(imageX);
  32.  
  33.  
  34. setVisible(true);
  35. }
  36.  
  37. public static void main(String args[]) {
  38. TicTacToe3 e = new TicTacToe3();
  39. }
  40.  
  41.  
  42. class ButtonListener extends MouseAdapter {
  43. public void mouseClicked(MouseEvent e){
  44. ImageIcon imageO = new ImageIcon("jon.jpg");
  45. button[i][j].setIcon(imageO);
  46.  
  47. }
  48. }
  49. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
treyd is offline Offline
25 posts
since Aug 2009
Nov 3rd, 2009
0
Re: HELP with code
You have to actually create the JButtons in your array. You have only declared the array of references with this statement
Java Syntax (Toggle Plain Text)
  1. JButton[][] button = new JButton[3][3];
You must still initialize those objects by adding
Java Syntax (Toggle Plain Text)
  1. button[i][j] = new JButton();
in your loop before you try to do anything with them.
Last edited by Ezzaral; Nov 3rd, 2009 at 3:40 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 3rd, 2009
0
Re: HELP with code
Thanks! That makes perfect sense. That solves the whole window popping up thing, but I have still am having two probelms. My initial image icon only shows in the first button. Also, when I click a buton I get a long list of errors. Something like:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Any ideas?
Thanks again
Reputation Points: 10
Solved Threads: 0
Light Poster
treyd is offline Offline
25 posts
since Aug 2009
Nov 3rd, 2009
0
Re: HELP with code
Okay. I got all the images in the right spot. Now I just need to get them to switch to the other image.

New Code:

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class TicTacToe3 extends JFrame{
  6.  
  7. JButton x;
  8. ImageIcon imageO;
  9. JButton o;
  10. ImageIcon imageX;
  11. JButton[][] button = new JButton[3][3];
  12. int i;
  13. int j;
  14.  
  15. TicTacToe3(){
  16. super("TicTacToe2");
  17. setSize(600, 300);
  18. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. setLocationRelativeTo(null);
  20. setLayout(new GridLayout(3,3));
  21.  
  22. JButton[][] button = new JButton[3][3];
  23. for(int i=0;i<3;i++){
  24. for(int j=0;j<3;j++){
  25. button[i][j] = new JButton();
  26. ImageIcon imageX = new ImageIcon("kate.jpg");
  27. button[i][j].setIcon(imageX);
  28. button[i][j].addMouseListener(new ButtonListener());
  29.  
  30. add(button[i][j]);
  31. }
  32. }
  33.  
  34.  
  35.  
  36. setVisible(true);
  37. }
  38.  
  39. public static void main(String args[]) {
  40. TicTacToe3 e = new TicTacToe3();
  41. }
  42.  
  43.  
  44. class ButtonListener extends MouseAdapter {
  45. public void mouseClicked(MouseEvent e){
  46. ImageIcon imageO = new ImageIcon("jon.jpg");
  47. button[i][j].setIcon(imageO);
  48.  
  49. }
  50. }
  51. }
Reputation Points: 10
Solved Threads: 0
Light Poster
treyd is offline Offline
25 posts
since Aug 2009
Nov 3rd, 2009
0
Re: HELP with code
Your listener is setting the icon on whichever button i and j corresponds to
Java Syntax (Toggle Plain Text)
  1. button[i][j].setIcon(imageO);
What you really want to do is getSource() from the mouse event and cast that to a JButton and set it's image.
Java Syntax (Toggle Plain Text)
  1. JButton clickedButton = (JButton)e.getSource();
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 3rd, 2009
0
Re: HELP with code
Where do I put that?
Reputation Points: 10
Solved Threads: 0
Light Poster
treyd is offline Offline
25 posts
since Aug 2009
Nov 3rd, 2009
0
Re: HELP with code
When I take out:
Java Syntax (Toggle Plain Text)
  1. button[i][j].setIcon(imageO);
and add:
Java Syntax (Toggle Plain Text)
  1. JButton clickedButton = (JButton)e.getSource();

It no longer has error, but it doesnt swith. How do I pass it an image?
Reputation Points: 10
Solved Threads: 0
Light Poster
treyd is offline Offline
25 posts
since Aug 2009
Nov 3rd, 2009
0
Re: HELP with code
Call methods on "clickedButton". I wrote that to show you how to obtain a reference to the button that was clicked.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 3rd, 2009
0
Re: HELP with code
Got it!

Thanks again for all the help!
Reputation Points: 10
Solved Threads: 0
Light Poster
treyd is offline Offline
25 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How do I change my repeated code to pass arguments?
Next Thread in Java Forum Timeline: Connecting to a mysql database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC