Hey everyone,
I'm trying to make a button have an image, it used to work but for some reason, now it doesn't.
This is the error:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Main.createImageIcon(Main.java:109)
at Main.<init>(Main.java:82)
at Main.main(Main.java:103)
I see that the error is the:
ImageIcon NewFileIcon = createImageIcon("Images/NewFileIcon.png");
ImageIcon LoadFileIcon = createImageIcon("Images/LoadFileIcon.png");
ImageIcon SaveFileIcon = createImageIcon("Images/SaveFileIcon.png");
ImageIcon OptionsIcon = createImageIcon("Images/OptionsIcon.png");
I see nothing wrong with it...
It's not in the main method or my Main method.
public static void main(){}
public Main(){}
It used to work which is what makes me think I'm stupid, it must be obvious but well... Not to me!
Important bits:
public class Main implements KeyListener{
ImageIcon NewFileIcon = createImageIcon("Images/NewFileIcon.png");
ImageIcon LoadFileIcon = createImageIcon("Images/LoadFileIcon.png");
ImageIcon SaveFileIcon = createImageIcon("Images/SaveFileIcon.png");
ImageIcon OptionsIcon = createImageIcon("Images/OptionsIcon.png");
public static void main(String[] args) {
if(args.length > 0){
fileLoadPath = args[0];
}
new Main();
}
public ImageIcon createImageIcon(String path) {
java.net.URL imgURL = Main.class.getResource(path);
return new ImageIcon(imgURL);
}
public Main(){
JButton New = new JButton(NewFileIcon);
//Other buttons too
}