So while making the following program I am getting the errors as mentioned below: I don't know why the IDE is not able to find my images and the images are in the same folder as the .java files. I am using eclipse neon
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at gui_22.gui.<init>(gui.java:24)
at gui_22.MAIN.main(MAIN.java:6)
The code is as follows:
Main Class
import javax.swing.JFrame;
public class MAIN(){
public static void main(String[] args);
gui go = new gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,200);
go.setVisible(true);
}
}
Gui Class
import java.awt.FlowLayout;
import.java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class gui extends JFrame{
private JButton reg;// to create buttons
private JButton custom;// Same as above
public gui(){
super("The Title"); //Allows access to the superclass.
setLayout(new FlowLayout());
reg = new JButton("reg Button");
add(reg);
Icon b = new ImageIcon(getClass().getResource("b.png"));
Icon x = new ImageIcon(getClass().getResource("a.png"));
custom = new JButton("Custom",b);
custom.setRolloverIcon(x);
add(custom);
HandlerClass handler = new HandlerClass();
reg.addActionListener(handler);//adding a handler for the button
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null,String.format("%s", event.getActionCommand()));
}
}
}