0

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.

2 Answers 2

2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I've done exactly as you said there and I am no longer getting errors, however the blank(white) images are simply not appearing on the frame.
2

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);

    }
});

2 Comments

Please do not post multiple answers on the same thread, instead edit this one to include the other two. This way you wont loose these points you have earned. Though, even though the GUI is started on the EDT, still calls like pack()/setVisible()` must come after adding all components to the container, not before that. Doing it before can give obnoxious results :-)
Here comes my +1, though regarding the path thingy, please refer to this answer :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.