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.

Malix
  • 1
  • 1
  • 2

2 Answers2

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);
}
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
  • 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. – Malix Aug 03 '13 at 13:28
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);

    }
});
Community
  • 1
  • 1
baratali
  • 443
  • 7
  • 16
  • Please do not post multiple answers on the same thread, instead edit this one to include the other two. This way you won`t 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 :-) – nIcE cOw Aug 03 '13 at 17:02
  • Here comes my +1, though regarding the path thingy, please refer to this [answer](http://stackoverflow.com/a/9866659/1057230) :-) – nIcE cOw Aug 04 '13 at 03:13