1

I'm trying to display 15 images on JButton and I tried to run it; however, I got this error message:

Exception in thread "main"java.lang.NullPointerException at 
           javax.swing.ImageIcon.<init>(ImageIcon.java:217). 

All the images are stored in same folder for GUIClass class.

Here is my code:

private JFrame frame = new JFrame(); // Create frame
private JButton[][]grid; // Name the grid of buttons

public GUIClass(int width, int length){
    frame.setLayout(new GridLayout(width,length));
    grid = new JButton[width][length];

    for(int y = 0; y < length; y++){
        for(int x = 0; x < width; x++){
            grid[x][y] = new JButton(new ImageIcon(this.getClass().getResource(x + ".png")) );
            frame.add(grid[x][y]);// Adds button to grid
        }
    }

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[]args){
     new SupplyHouseTest(16,16);// Makes new ButtonGrid with 2 parameters
}

I tried new ImageIcon(this.getClass().getResource(Integer.toString(x) + ".png")) but I still get the same error.

All the images are stored in Source Package.

PJU
  • 25
  • 8
  • Where are you images stored and what are there names? – MadProgrammer Oct 14 '15 at 05:00
  • You must have `x` number of images, named `0.png` through `{x}.png` in the `/images` directory – MadProgrammer Oct 14 '15 at 05:01
  • Your for loop will loop though (length) number of times. Do you have images in the image directory to fulfil this? (1.png, 2.png, 3.png ... length.png) – Achintha Gunasekara Oct 14 '15 at 05:03
  • @AchinthaGunasekara - All the images are stored in Source Package. Yes, all images are number in order. – PJU Oct 14 '15 at 05:13
  • @PJU *"Yes, all images are number in order"* from what to what? – MadProgrammer Oct 14 '15 at 05:15
  • All images that are referenced by your for loop must be present. E.G: 1.png, 2.png, 3.png ... length.png. Do you have them all? – Achintha Gunasekara Oct 14 '15 at 05:18
  • @PJU, what is name of the package you are using for storing those png? –  Oct 14 '15 at 05:18
  • Please note that your latest edit completely changes the whole issue - there is a significant difference whether your path starts with `/` or not, when using `Class.getResource()`. See http://stackoverflow.com/questions/6608795/what-is-the-difference-between-class-getresource-and-classloader-getresource -`Class.getResource()` searches **relative** to the class itself. Where exactly have you stored the images? Ideally add a diagram of your project directory tree to the question. – Andreas Fester Oct 14 '15 at 05:20
  • @AchinthaGunasekara: Yes, I do have all of them between 0.png to 14.png. – PJU Oct 14 '15 at 05:20
  • In that case, assuming length value that was passed in is 15 (it'll throw this error for any value higher than 15 for length because x will go higher than 14), have you tried adding the full path for just to debug your issue? EG: /var/local/myapp/x.png or c:\\tmp\\myapp\\x.png – Achintha Gunasekara Oct 14 '15 at 05:23
  • @PJU I did a basic test with your code, having the images in the same directory as the class file and it seems to work just fine. Make sure that the `width` value is `< 15` and add some debug statements to see what is actually getting load. – MadProgrammer Oct 14 '15 at 05:25
  • @AchinthaGunasekara, ahh, I changed the x from 16 to 15 and it worked. Thank you for be patient with me! =) – PJU Oct 14 '15 at 05:25
  • @MadProgrammer, yes you're correct and I just fixed and now it's working. Thank you very much! – PJU Oct 14 '15 at 05:26

0 Answers0