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