images, layers, labels and panels.. something's wrong somewhere, please help!
I made a class called imgObj, so that I could add custom attributes to an Image, such as name, position and layer (front, back, ect.)
Before further explaination, I'll just post the code:
Code :
public class Window extends JFrame {
private ArrayList<imgObj> allImages;
private JLabel[] labels;
private JPanel[] panels; //<-- contains the labels.
private JLayeredPane aLPane = new JLayeredPane() ; //<-- contains the panel.
public Window(){
super("title");
setLayout(new BorderLayout());
add(aLPane, BorderLayout.CENTER);
aLPane.setBounds(0,0,800,600);
LoadImages();
labels = new JLabel[allImages.size()];
panels = new JPanel[allImages.size()];
//loop through allImages, and give all images a label, and adds the label to the window.
int counter = 0;
for(imgObj i : allImages){
labels[counter] = new JLabel(i.getImg());
labels[counter].setOpaque(true);
labels[counter].setBackground(Color.BLUE);
labels[counter].setBounds(i.getPosx(), i.getPosy(), i.getImg().getIconWidth(),i.getImg().getIconHeight());
panels[counter].add(labels[counter]);
panels[counter].setOpaque(true);
panels[counter].setBounds(0, 0, 800, 600);
panels[counter].setBackground(Color.BLACK);
System.out.printf("%s was added to window, as pos (%d ; %d)\n", i.getName(), i.getPosx(), i.getPosy());
counter++;
aLPane.add(panels[counter], new Integer(i.getLayer()), 0);
}
}
}
My problem is: when I run the code, I get a NullPointerException at the "panels[counter].add(labels[counter]);".
When looking at the variables in the debug-window (i am using Eclipse), I can see that both my imgObj icons are loaded correctly, and it seems that nothing is wrong the first time the enhanced for-loop is running (yet the console does not print " ... was added to window at pos..."), but the second time, the i.getImg() in "labels[counter] = new JLabel(i.getImg());" is null.
Any help will (of course) be appriciated, and I will gladly elaporate anything which I have (tried to) explained in this first post..
Re: images, layers, labels and panels.. something's wrong somewhere, please help!
When you debug, look at the value of panel[counter]....an array of JPanel's is created, but the members of that array must each be instantiated.
Re: images, layers, labels and panels.. something's wrong somewhere, please help!
Yep, that was it, #2!
Thank you!