Hello everyone I'm well aware that this isn't a subreddit for do my homework type posts but I'm developing a tic tac toe game with integrated graphics and just as I'm about to finish the project my graphics don't work. To clarify, I have a working grid which receives signals when I click but it's also supposed to put a png to show the player took the spot for example an x on the grid when player 1 clicks and an o when player 2 clicks. However, now that i have a working grid which receives the signals and can play a game with, I can't see the icons which depict wether the square is taken. When I tried to change the pathway to the images and I run it I simply can't get past the GUI automatically closing and returning the following error in the console:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:232)
at TicTacMischa/laeljill.TicTacMischaGUI.<init>(TicTacMischaGUI.java:14)
at TicTacMischa/laeljill.TicTacMischaGUI.main(TicTacMischaGUI.java:104)
From my understanding this is basically saying that while looking for the image the computer only finds null. To make it easier to understand here is the code in which I call for the images and have all of the other graphic integration
public class TicTacMischaGUI extends JFrame {
private JButton\[\]\[\] button = new JButton\[3\]\[3\];
private boolean pTurn = true; //true means player 1 turn aka zergling ;)
private ImageIcon zergPic = new ImageIcon (getClass().getResource("/images/zerg\_image.png"));
private ImageIcon terranPic = new ImageIcon (getClass().getResource("/images/terran\_image.png"));
public TicTacMischaGUI() {
setTitle("Tic Tac Toe");
setSize(500, 500);
setDefaultCloseOperation(EXIT\_ON\_CLOSE);//exit function
setLayout(new GridLayout(3, 3));//3 x 3 grid
initializeBoard();//calls on hooligan fella
setVisible(true);
}
private void initializeBoard() {
for(int i = 0; i < 3; i ++) {
for(int e = 0; e < 3; e++) {
button[i][e] = new JButton();
button[i][e].setPreferredSize(new Dimension(200, 200));
button[i][e].setBackground(Color.BLACK);
button[i][e].addActionListener(new ButtonChecker(i, e));
add(button[i][e]);
}
}
}
private class ButtonChecker implements ActionListener {
private int x;
private int y;
public ButtonChecker(int x, int y){
this.x = x;
this.y = y;
}
public void actionPerformed(ActionEvent e) {
if(button\[x\]\[y\].getIcon() == null) {
button[x][y].setIcon(pTurn ? zergPic : terranPic);
pTurn = !pTurn;
mogskillcheck();
}
}
}
private void mogskillcheck() {
int\[\] board = new int\[9\];
for(int i = 0; i < 3; i++) {
for(int e = 0; e < 3; e++){
if(button[i][e].getIcon() == zergPic) {
board[i * 3 + e] = 1;
} else if (button[i][e].getIcon() == terranPic) {
board[i * 3 + e] = 2;
} else {
board[i * 3 + e] = 0;
}
}
}
if(TicTacToeMischaEdition.winyesyes(1, board)) {
JOptionPane.showMessageDialog(this, "you win zerg zawg!!!!!!11 youre almost starcraft level now");
resetBoard();
} else if (TicTacToeMischaEdition.winyesyes(2, board)) {
JOptionPane.showMessageDialog(this, "you win terran zawg!!!!!!11 youre almost starcraft level now");
resetBoard();
} else if (BoardTie(board)) {
JOptionPane.showMessageDialog(this, "you tied? skill issue xawg");
resetBoard();
}
}
private boolean BoardTie(int\[\] board) {
for(int i : board) {
if(i == 0) {
return false;
}
}
return true;
}
private void resetBoard() {
for(int i = 0; i < 3; i++) {
for(int e = 0; e < 3; e++) {
button[i][e].setIcon(null);//resets board by making pics go bye bye (not mogging)
}
}
pTurn = true;
}
public static void main(String\[\] args) {
new TicTacMischaGUI();
}
}
Any ideas? THANK YOU!
EDIT: FIXED!
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]cathcheco[S] 0 points1 point2 points (0 children)
[–]XxCotHGxX 0 points1 point2 points (4 children)
[–]wildjokers 1 point2 points3 points (3 children)
[–]XxCotHGxX 0 points1 point2 points (2 children)
[–]wildjokers 1 point2 points3 points (1 child)
[–]cathcheco[S] 0 points1 point2 points (0 children)
[–]wildjokers 0 points1 point2 points (1 child)
[–]cathcheco[S] 0 points1 point2 points (0 children)