| | |
HELP with code
![]() |
•
•
Join Date: Aug 2009
Posts: 25
Reputation:
Solved Threads: 0
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:
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)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TicTacToe3 extends JFrame{ JButton x; ImageIcon imageO; JButton o; ImageIcon imageX; JButton[][] button = new JButton[3][3]; int i; int j; TicTacToe3(){ super("TicTacToe2"); setSize(600, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setLayout(new GridLayout(3,3)); JButton[][] button = new JButton[3][3]; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ button[i][j].addMouseListener(new ButtonListener()); add(button[i][j]); } } ImageIcon imageX = new ImageIcon("kate.jpg"); button[i][j].setIcon(imageX); setVisible(true); } public static void main(String args[]) { TicTacToe3 e = new TicTacToe3(); } class ButtonListener extends MouseAdapter { public void mouseClicked(MouseEvent e){ ImageIcon imageO = new ImageIcon("jon.jpg"); button[i][j].setIcon(imageO); } } }
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 You must still initialize those objects by adding in your loop before you try to do anything with them.
Java Syntax (Toggle Plain Text)
JButton[][] button = new JButton[3][3];
Java Syntax (Toggle Plain Text)
button[i][j] = new JButton();
Last edited by Ezzaral; 23 Days Ago at 3:40 pm.
•
•
Join Date: Aug 2009
Posts: 25
Reputation:
Solved Threads: 0
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
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Any ideas?
Thanks again
•
•
Join Date: Aug 2009
Posts: 25
Reputation:
Solved Threads: 0
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:
New Code:
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TicTacToe3 extends JFrame{ JButton x; ImageIcon imageO; JButton o; ImageIcon imageX; JButton[][] button = new JButton[3][3]; int i; int j; TicTacToe3(){ super("TicTacToe2"); setSize(600, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setLayout(new GridLayout(3,3)); JButton[][] button = new JButton[3][3]; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ button[i][j] = new JButton(); ImageIcon imageX = new ImageIcon("kate.jpg"); button[i][j].setIcon(imageX); button[i][j].addMouseListener(new ButtonListener()); add(button[i][j]); } } setVisible(true); } public static void main(String args[]) { TicTacToe3 e = new TicTacToe3(); } class ButtonListener extends MouseAdapter { public void mouseClicked(MouseEvent e){ ImageIcon imageO = new ImageIcon("jon.jpg"); button[i][j].setIcon(imageO); } } }
0
#5 23 Days Ago
Your listener is setting the icon on whichever button i and j corresponds to 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)
button[i][j].setIcon(imageO);
Java Syntax (Toggle Plain Text)
JButton clickedButton = (JButton)e.getSource();
•
•
Join Date: Aug 2009
Posts: 25
Reputation:
Solved Threads: 0
0
#7 23 Days Ago
When I take out:
and add:
It no longer has error, but it doesnt swith. How do I pass it an image?
Java Syntax (Toggle Plain Text)
button[i][j].setIcon(imageO);
Java Syntax (Toggle Plain Text)
JButton clickedButton = (JButton)e.getSource();
It no longer has error, but it doesnt swith. How do I pass it an image?
![]() |
Similar Threads
- Code Snippet: Sample code for taking user input from Shell (Java)
- Code Snippet: code to split (and reconnect) an Access database (part 1) (Visual Basic 4 / 5 / 6)
- Code Snippet: Code that stores and retrieves. (C)
- Code Snippet: Efficient code for extracting unique elements from sorted array. (C)
- Code Snippet: Strings: Looking at ASCII Code (C)
- Code Snippet: Morse Code (C)
- Code Snippet: Gray Code Conversion (C)
- Code Snippet: code to validate data in Access withOUT using messageboxes (Visual Basic 4 / 5 / 6)
- Code Snippet: code to split (and reconnect) an Access database (part 2) (Visual Basic 4 / 5 / 6)
- Code Snippet: Send HTML code to Internet Explorer from basic (Legacy and Other Languages)
Other Threads in the Java Forum
- Previous Thread: How do I change my repeated code to pass arguments?
- Next Thread: Connecting to a mysql database
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






