0

Simple tic tac toe seperated into two classes. I'm aware of using a URL but i want to get this working. Here is the program so far. i got it from a youtube tutorial for the most part, but now the error message

Exception in thread "main" java.lang.NullPointerException

at java.desktop/javax.swing.ImageIcon.(Unknown Source)

at XOButton.(XOButton.java:17)

at TicTacToe.(TicTacToe.java:24)

at TicTacToe.main(TicTacToe.java:13)

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridLayout;



public class TicTacToe extends JFrame{

JPanel p = new JPanel();
XOButton buttons[] = new XOButton [9];

public static void main(String[] args) {
    new TicTacToe();
}

public TicTacToe() {
    super ("TicTacToe");
    setSize(400,400);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    p.setLayout(new GridLayout(3,3));
    for (int i=0; i<9; i++) {
        buttons[i] = new XOButton();
        p.add(buttons[i]);
    }

    add(p);

    setVisible(true);
}

}

next:

import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class XOButton extends JButton implements ActionListener{

ImageIcon X,O;
byte value = 0;
/*
 0:nothing
 1:X
 2:O
 */

public XOButton() {
    X = new ImageIcon(this.getClass().getResource("C:\\Users\\mattt\\Pictures\\X.PNG"));
    O = new ImageIcon(this.getClass().getResource("C:\\Users\\mattt\\Pictures\\0.PNG"));
    this.addActionListener(this);
}


public void actionPerformed(ActionEvent e) {
    value++;
    value %= 3;

    switch(value) {
    case 0:
        setIcon(null);
        break;
    case 1:
        setIcon(X);
    case 2:
        setIcon(O);
    }
}

}
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ken White May 10 '18 at 02:27
  • It is similar, but this has more to do with variable scope I think, i'm jus not sure, I looked at that and It didnt work out, the variables wont transfer to actionPerformed class if i initialize them inside XOButton method, but it also wont work if i initialize and turn them to obj before the method and i were to put inside simply X; – Matt Taliancich May 10 '18 at 03:26
  • So in conclusion for anybody else who looks at this, i fixed the break statements, and added a resource folder as a main folder in the project and switched this statement X = new ImageIcon(this.getClass().getResource("C:\\Users\\mattt\\Pictures\\X.PNG")); to the statement X = new ImageIcon("Resource/X.PNG"); and same for O and of course the 0 instead of O was a mistake as well, due to file name – Matt Taliancich May 10 '18 at 07:26

1 Answers1

0

There are some issues in your code.

1) To prevent nullpointerException you have to add the images into same package. and do the changes as below.

public XOButton() {
    X = new ImageIcon(this.getClass().getResource("X.PNG"));
    O = new ImageIcon(this.getClass().getResource("0.PNG"));
    this.addActionListener(this);
}

2) In your switch case statement case 1 and 2 doesn't has break statement.

public void actionPerformed(ActionEvent e) {
    value++;
    value %= 3;

    switch(value) {
    case 0:
        setIcon(null);
        break;
    case 1:
        setIcon(X);
        break;
    case 2:
        setIcon(O);
        break;
    default:
        break;
    }
}
MinA
  • 405
  • 4
  • 9
  • I fixed the breaks, stupid mistake, but are you saying put the images into the src for eclipse or anywhere in the package, i heard somewhere there is a resource folder in src but i cant find it on mine. – Matt Taliancich May 10 '18 at 03:39
  • @MattTaliancich you can create a resource folder there and put images to that. to create folder: https://stackoverflow.com/questions/27934796/how-do-i-add-a-resources-folder-to-my-java-project-in-eclipse If my answer helps please accept. So it will help for someone in the future. – MinA May 10 '18 at 03:50
  • Should be almost there the changes made was adding the resource folder and putting "Resouce/X.PNG" still giving me the error, almost feels like its just my eclipse that's not working to be able to find it at this point. Everything ive read here and elsewhere says it should be working. – Matt Taliancich May 10 '18 at 04:04
  • If you created a resource folder as mentioned then you have to make the following change as well;. X = new ImageIcon("resources\\X.PNG"); I have tested your code and it is working for me. – MinA May 10 '18 at 04:17
  • NO more error thank God! one last thing, i don't think i'm calling the X and O right in the actionPerformed part because they are not changing to the images. – Matt Taliancich May 10 '18 at 04:24
  • @MattTaliancich i think you have use zero instead of letter 'O' as image name. – MinA May 10 '18 at 05:37
  • I changed that when i realized, still doenst work, im like done, ill try to look further tomorrow. sigh, even tried a different picture. – Matt Taliancich May 10 '18 at 06:30
  • well, it is working for me. for 1 click picture X display and for 2 clicks on same cell picture O displays. :) – MinA May 10 '18 at 06:33
  • I Just got it to Work!! I was putting the Resource folder inside the src folder. It needed to be next to the src folder in the GUITraining project folder. i had put it there before but with the other error i couldn't find this out :D – Matt Taliancich May 10 '18 at 07:23