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.