HELP with code

Reply

Join Date: Aug 2009
Posts: 25
Reputation: treyd is an unknown quantity at this point 
Solved Threads: 0
treyd treyd is offline Offline
Light Poster

HELP with code

 
0
  #1
23 Days Ago
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:

  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #2
23 Days Ago
You have to actually create the JButtons in your array. You have only declared the array of references with this statement
  1. JButton[][] button = new JButton[3][3];
You must still initialize those objects by adding
  1. button[i][j] = new JButton();
in your loop before you try to do anything with them.
Last edited by Ezzaral; 23 Days Ago at 3:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 25
Reputation: treyd is an unknown quantity at this point 
Solved Threads: 0
treyd treyd is offline Offline
Light Poster
 
0
  #3
23 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 25
Reputation: treyd is an unknown quantity at this point 
Solved Threads: 0
treyd treyd is offline Offline
Light Poster
 
0
  #4
23 Days Ago
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:

  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #5
23 Days Ago
Your listener is setting the icon on whichever button i and j corresponds to
  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.
  1. JButton clickedButton = (JButton)e.getSource();
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 25
Reputation: treyd is an unknown quantity at this point 
Solved Threads: 0
treyd treyd is offline Offline
Light Poster
 
0
  #6
23 Days Ago
Where do I put that?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 25
Reputation: treyd is an unknown quantity at this point 
Solved Threads: 0
treyd treyd is offline Offline
Light Poster
 
0
  #7
23 Days Ago
When I take out:
  1. button[i][j].setIcon(imageO);
and add:
  1. JButton clickedButton = (JButton)e.getSource();

It no longer has error, but it doesnt swith. How do I pass it an image?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #8
23 Days Ago
Call methods on "clickedButton". I wrote that to show you how to obtain a reference to the button that was clicked.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 25
Reputation: treyd is an unknown quantity at this point 
Solved Threads: 0
treyd treyd is offline Offline
Light Poster
 
0
  #9
23 Days Ago
Got it!

Thanks again for all the help!
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC