0

I've been trying for hours to be able to be able to give a label a picture if it has a blank image ('blank.jpg') and every time i'm coming up with either an out of bounds error or a null pointer error (currently). Can anyone help me out and check my code for the problem:

public class HomeController extends JFrame
{

  public HomeController()
  {
    JFrame main = new JFrame();
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setTitle("Smart Home Control Panel");       

    //PANEL CONTAINING THE PICTURES
    JPanel labelPanel = new JPanel();

    labelPanel.setLayout(new GridLayout(3,3));
    labelPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    ImageIcon blank = new ImageIcon("blank.jpg");
    ImageIcon lamp = new ImageIcon("lamp.jpg");
    ImageIcon clock = new ImageIcon("clock.jpg");
    ImageIcon television = new ImageIcon("television.jpg");

    JLabel pictures[]=new JLabel[9];
    for (int i=0;i<=pictures.length; i++)
    {
        pictures[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
        pictures[i].setIcon(blank);
        labelPanel.add(pictures[i]);
    }


    main.add(labelPanel, BorderLayout.CENTER);
    //----------------------------------------------------------


    //PANEL CONTAINING BUTTONS
    JPanel buttonPanel = new JPanel();

    JButton but1 = new JButton("ADD APPLIANCE");
    JButton but2 = new JButton("CLEAR SCREEN");
    JButton but3 = new JButton("LOAD FILE");
    JButton but4 = new JButton("SAVE FILE");

    buttonPanel.add(but1);
    buttonPanel.add(but2);
    buttonPanel.add(but3);
    buttonPanel.add(but4);


    main.add(buttonPanel, BorderLayout.PAGE_END);
    //-------------------------------------------

    //EVENT HANDLER FOR BUTTONS
    but1.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {

            Object[] options = {"Clock",
                    "Lamp",
                    "Television"};
            int n = JOptionPane.showOptionDialog(main,
            "Which type of appliance?  "
            + "",
            "",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[2]);
            if (n == JOptionPane.YES_OPTION)
            {
                setAvailable(clock);
            }


        }


        public void setAvailable(ImageIcon clock) 
        {
            for (int i=0; i<=pictures.length; i++)
            {
                if(pictures[i].getIcon().toString() == "blank.jpg")
                {
                    pictures[i].setIcon(clock);
                }
            }


        }

    }

    //-------------------------
 }     

}
Mark Stewart
  • 2,046
  • 4
  • 22
  • 32
  • not a duplicate. In fact I read it and it did not provide me with an answer. I simply want someone to look over my code and recognize MY error. – andrew fenny Apr 27 '16 at 13:56
  • Your `JLabel pictures[]=new JLabel[9];` array is not initialized. But you are using it in your `for` loop. – ujulu Apr 27 '16 at 14:20
  • `i<=pictures.length;` will throw a IndexOutOfBoundsException exception when `i = pictures.length`. – Đăng Khoa Huỳnh Apr 27 '16 at 15:25

0 Answers0