| | |
HELP with code
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 31 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; 31 Days Ago at 3:40 pm.
•
•
Join Date: Aug 2009
Posts: 25
Reputation:
Solved Threads: 0
0
#3 31 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 31 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 31 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 31 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 |
3d @param affinetransform android api applet application arc arguments array arrays automation binary bluetooth byte capture chat class classes click client code color compare component count database design detection eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer j2me java java.xls javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong print problem producer program programming project projectideas read recursion replaysolutions rim scanner screen server set size sms sort sql string swing terminal threads time transforms tree ui web windows






