I'm doing a small GUI program it is that I have two buttons when I press on any of the buttons it displays a dialog box saying which button I pressed .One of the buttons is a text with image displayed on it . my problem is that an exception occurs when the program runs saying NULL pointer exception means it can't reach the image given in IMageIcon how can I solve this problem if I want to display an image from my computer on button and here is code :
Button Frame class
public class ButtonFrame extends JFrame{
private JButton plainJButton;
private JButton fancyJButton;
public ButtonFrame()
{
super("Testing Buttons");
setLayout(new FlowLayout());
plainJButton=new JButton("Plain Button");
add(plainJButton);
Icon bug1=new ImageIcon(getClass().getResource("20072011001.jpeg"));
Icon bug2=new ImageIcon(getClass().getResource("20072011002.jpeg"));
fancyJButton=new JButton("Fancy Button",bug1);
fancyJButton.setRolloverIcon(bug2);
add(fancyJButton);
ButtonHandler handler=new ButtonHandler();
fancyJButton.addActionListener(handler);
plainJButton.addActionListener(handler);
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(ButtonFrame.this,String.format("you pressed %s", e.getActionCommand()));
}
}
Main.java
ButtonFrame buttonFrame=new ButtonFrame();
buttonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttonFrame.setSize(275, 110);
buttonFrame.setVisible(true);