2

im new to java, and while running this on eclipse linux mint with java-7-openjdk (i think , although dont know if eclipse has its own sun jdt),

public class Gui extends JFrame{
private JComboBox box;
private JLabel picture;
private static String[] filename = {"b.PNG","x.PNG"};
private Icon[] pics = {new ImageIcon(getClass().getResource(filename[0])),   
    new ImageIcon(getClass().getResource(filename[1]))};



Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at bucky.Gui.<init>(Gui.java:10)
at bucky.apples.main(apples.java:12)

I cant see what the problem is (linux mint 15 cinnamon)

  • check that getClass().getResource(filename[0]) is actually going to the path of your image... – gtgaxiola Aug 07 '13 at 13:39
  • Simply place `b.PNG` and `x.PNG` alongside `GUI.class` file... More info can be found on this [answer](http://stackoverflow.com/a/9866659/1057230) :-). Hope it helps :-) – nIcE cOw Aug 07 '13 at 13:52

3 Answers3

2

This Exception is thrown in ImageIcon constructor and states that getClass().getResource(filename[0]) or getClass().getResource(filename[1]) is null

The path you specified isn't correct. Make sure folder where these images are is in your classpath

An easy check can be made:

File f = new File("b.PNG");
System.out.println(f.exists());

should print you true

Tala
  • 8,888
  • 5
  • 34
  • 38
2

getClass().getResource(filename[0]) where filename[0] is b.PNG denotes a relative path and will try to load the image from the same folder (i.e. the same package) as your class Gui.

Make sure the images are actually at that location.

If your images are at the default package (i.e. your root source folder) you can load them as getClass().getResource("/" + filename[0]) (i.e. using an absolute path).

c.s.
  • 4,786
  • 18
  • 32
0

Oh human mistakes can be so mean. Instead of b.PNG and x.PNG, my files were a.PNG and x.PNG Really a shame, but thx nonetheless.