-1

I want to know why I am having this problem when I literally copied and pasted this section of code from one Java project to another, and didn't run into any issues in the first project.

I am attempting to display a 4x4 grid of squares.

The following exception is thrown on line 37:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!

public class DistractorTask {

    private JPanel panel;
    private GridBagConstraints c;
    private BufferedImage squareImage;

    public DistractorTask(JPanel panel){
        this.panel = panel;
        c = new GridBagConstraints();
        displayGrid();

    }

    private void displayGrid() {


        c.gridx = 0;
        c.gridy = 0;

        try {
            squareImage = ImageIO.read(this.getClass().getResource("square.gif"));  //line 37
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        JLabel squareLabel = new JLabel(new ImageIcon(squareImage));

        for(int i = 0; i < 16; i++){
            c.gridx = i % 4;
            c.gridy = i / 4;
            panel.add(squareLabel, c);
            panel.validate();

        }

    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
lb91
  • 109
  • 9

1 Answers1

0

Your code, it's trying to load the image from the class path. Try moving the imagine into the same folder as your compiled DistractorTask.class

Minh Kieu
  • 475
  • 3
  • 9
  • Do you mean I have to move the image into the src folder? I'm confused as to why I have to do this because in previous projects I haven't run into this problem before (I usually have an image folder that it is at the same level as the src folder, and it works fine without this exception being thrown). – lb91 May 01 '16 at 06:21
  • It does depends on whether your IDE copy the image into the compiled folder during the compilation process. Just for debugging purpose, try copy the image into this folder to verify its working. Then you can look into ways of copying resources... – Minh Kieu May 01 '16 at 06:25
  • I did as you said, and the exception is not being thrown anymore. However, my program isn't actually doing anything. – lb91 May 01 '16 at 06:27
  • Ok, so Java found the image. Now verify that it's loading the image by checking it size. If the image is loaded then you will need to debug your code to see why the image is not displaying. – Minh Kieu May 01 '16 at 06:35
  • I just realised a reason why it wasn't displaying. Either it was because I hadn't resized the image or it was completely white. I know because I tried with another image and it loaded. – lb91 May 01 '16 at 06:36
  • Do you know how I can store the image in a separate "img" folder without running into this problem? I'm curious as to why it worked previously. – lb91 May 01 '16 at 06:37
  • The code you used looks for the image in the class path. If you want to look for resources in a physical location then why not specify that? The nice thing about looking for resources in the class-path is that you can run it in a jar and/or deploy anywhere. You just need to ensure all depend resources get copied and make available to Java in its class-path – Minh Kieu May 01 '16 at 06:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110745/discussion-between-lb91-and-minh-kieu). – lb91 May 01 '16 at 08:37