-1

Hey so I've moved my images to the correct work space folder(src) but i keep getting this error message.....

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at GUI.<init>(GUI.java:20)
    at main.main(main.java:4)

This is the code

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.FlowLayout;

public class GUI extends JFrame {
    private JButton reg;
    private JButton custom;
    public GUI(){
        super("Welcome");
        setLayout(new FlowLayout());
        reg = new JButton("reg button");
        add(reg);

        Icon b = new ImageIcon(getClass().getResource("b.png"));
        Icon a = new ImageIcon(getClass().getResource("a.png"));
        custom = new JButton("Custom", b);
        custom.setRolloverIcon(a);
        add(custom);

        thehandler handle = new thehandler();
        reg.addActionListener(handle);
        custom.addActionListener(handle);

    }

    private class thehandler implements ActionListener{
        public void actionPerformed(ActionEvent event){
            JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
        }
    }

}


-----------------------

import javax.swing.JFrame;
public class main {
public static void main(String agrs[]){
    GUI page = new GUI();
    page.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    page.setSize(300,200);
    page.setVisible(true);
}
}
Barett
  • 5,826
  • 6
  • 51
  • 55
Derek MC
  • 366
  • 2
  • 4
  • 25
  • did you try `"/b.png"` ? – sanbhat Aug 02 '13 at 17:17
  • 0 down vote Kindly try giving the full path name of your src folder along with the image file name. – G.Srinivas Kishan Aug 02 '13 at 17:21
  • 2
    No Didn't work. Thanks anyway. :-) – Derek MC Aug 02 '13 at 17:21
  • @user2422456 Can you tell us where exactly the Images are located? May be this can help you [--> how to get a conf.txt path in eclipse](http://stackoverflow.com/questions/17307817/how-to-get-a-conf-txt-path-in-eclipse/17308408#17308408) – Smit Aug 02 '13 at 17:30
  • See also [tag:embedded-resource] info. – trashgod Aug 03 '13 at 01:40
  • @user2422456 : Please have a look at this answer, [Load ImageIcon Exception](http://stackoverflow.com/a/9866659/1057230), hopefully this might be of some help :-) – nIcE cOw Aug 03 '13 at 03:19
  • possible duplicate of [Load Icon Image Exception](http://stackoverflow.com/questions/9864267/load-icon-image-exception) – Barett Jun 24 '15 at 17:48

2 Answers2

2

you must put your image in the folder that contains src to use directly, not in src itself.

Paniz
  • 594
  • 6
  • 19
  • @user2422456 : The way you accessing it right now i.e. `getClass().getResource("b.png")`, for this to work, you have to put the image, along side the `.class` file, though if you write `getClass().getResource("/b.png")`, the image should be alongside the package folder. The comment posted above explains the whole thingy in a better way, than I can in this small comment's space :-) – nIcE cOw Aug 03 '13 at 03:24
1

The Image Should Be Visible Under Src->DefaultPackage->**File.java,FileMain.java,b.png,x.png; if the image file are not visible under **Src->DefaultPackage-> then Press F5 Button or just right click on the Package Explorer and Click Refresh That Should Make Your Image Appear In the Src->DefaultPackage->.