简体   繁体   中英

Java - Filling an array of JLabels with ImageIcons via loop(NullPointerException)

the basic aim is to have a JPanel filled with 9 white squares in a 3x3 pattern; The squares are 150x150 blank white .jpg files. It must be this way since later on, the program will have to change the blank squares to one of a selection of simple images, and must be able to change any square at any time. The problem, simply, is I'm getting a NullPointerException. I have to assume it's something to do with initialising the array as null but NetBeans(yes, NetBeans...) seems to get angry with me if I don't do that. Same if I try to declare the size of the array. (That would be... "ArrayType[arraysize] arrayName;", yes?"

Egh, I'm just guessing wildly.

Edit - NullPointerException fixed, but now the blank(white) images are simply not appearing in the frame . Code below edited to reflect its new state, more potentially relevant lines added.

Here be all relevant code:

JFrame controller = new JFrame("SmartHome Interface");
controller.setVisible(true);
controller.setSize(480,500);
controller.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//[...]

JPanel labelPanel = new JPanel();

//[...]

labelPanel.setBackground(Color.GREEN);

//[...]

ImageIcon blank = new ImageIcon("../Images/blank.jpg");

//[...]

controller.add(labelPanel);

//[...]

JLabel[] labels = new JLabel[9];
        for (int i = 0; i <= 8; i++)
        {
            int xLowBound;
            int xUpBound;
            int yLowBound; 
            int yUpBound;

            //Maths for positioning the labels correctly. Should be 150px in size with 10px gaps each.
            xLowBound = (i % 3) * 160;
            xUpBound = xLowBound + 150;
            yLowBound = (i / 3) * 160;
            yUpBound = yLowBound + 150;

            labels[i] = new JLabel();
            labels[i].setIcon(blank);
            labels[i].setBounds(xLowBound, yLowBound, xUpBound, yUpBound);
            labelPanel.add(labels[i]);
        }

Also.....is the filepath for the ImageIcon correct? The code itself being located in "src/smarthome" and the images in "src/Images"

And apologies if I broke any forum conventions/codes of conduct/etc. Newby here, tried to be careful not to but I may have forgotten something.

Your problem reduces to this:

JLabel[] labels = null;
for (int i = 0; i <= 8; i++) {
    labels[i].setIcon(blank);
}

This code fragment will fail because labels == null. Therefore labels[i] == null.

Use this instead:

JLabel[] labels = new JLabel[9];
for (int i = 0; i <= 8; i++) {
    labels[i] = new JLabel();
    labels[i].setIcon(blank);
}

Your filepath for imageIcons is incorrect. You should use:

ImageIcon img = new ImageIcon(getClass().getResource("../Images/blank.jpg"));    

if your code is in static method use this:

ImageIcon img = new ImageIcon(YourClass.class.getResource("../Images/blank.jpg"));

There is a good answer about loading image icons(thanks to nIcE cOw ).


You should call setVisible() and setSize() after adding all components to the frame.


Add components to frame's content pane( frame.getContentPane() ).


You always should place your GUI code in separate thread.


So, your code will be:

SwingUtilities.invokeLater(new Runnable()
{

    @Override
    public void run()
    {
        JFrame controller = new JFrame("SmartHome Interface");
        controller.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel labelPanel = new JPanel();

        labelPanel.setBackground(Color.GREEN);

        // !!!
        ImageIcon blank = new ImageIcon(YourClass.class
                .getResource("../Images/blank.jpg"));

        // !!!
        controller.getContentPane().add(labelPanel);

        JLabel[] labels = new JLabel[9];
        for (int i = 0; i <= 8; i++)
        {
            int xLowBound;
            int xUpBound;
            int yLowBound;
            int yUpBound;

            xLowBound = (i % 3) * 160;
            xUpBound = xLowBound + 150;
            yLowBound = (i / 3) * 160;
            yUpBound = yLowBound + 150;

            labels[i] = new JLabel();
            labels[i].setIcon(blank);
            labels[i].setBounds(xLowBound, yLowBound, xUpBound,
                    yUpBound);
            labelPanel.add(labels[i]);
        }

        // !!!    
        controller.setVisible(true);
        controller.setSize(480, 500);

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM