I have a uni course that involves writing small Java applications wtih Apache Netbeans and Swing. Naturally instead of having an introduction course they had us start off immediately so I'm essentially a complete beginner (bout 2 weeks in). With these excuses out of the way here comes the qustion. We use the Apache Netbeans interface to design a small application and then write the logic behind all the components. Since this week's project involved making some sort of a game I decided to go for Space Invaders. For that I need to have 25 JLabels with an icon each. Manipulating those 25 JLabels in bulk with for loops is what I can't figure out how to do. If I want to move 5 of them to a different position on the panel I have to type their names out manually which just doesn't seem efficient enough:
private void setSprites(){
int x = 240;
int y = 80;
int xSize = 64;
int ySize = 64;
int gapSize = 48;
en3Ship1.setLocation(x, y);
x = x + xSize;
en3Ship2.setLocation(x, y);
x = x + xSize;
en3Ship3.setLocation(x, y);
x = x + xSize;
en3Ship4.setLocation(x, y);
x = x + xSize;
en3Ship5.setLocation(x, y);
x = 240;
y = y + ySize;
en2Ship1.setLocation(x, y);
x = x + xSize;
en2Ship2.setLocation(x, y);
x = x + xSize;
en2Ship3.setLocation(x, y);
x = x + xSize;
en2Ship4.setLocation(x, y);
x = x + xSize;
en2Ship5.setLocation(x, y);
x = 240;
y = y + ySize;
en2Ship6.setLocation(x, y);
x = x + xSize;
en2Ship7.setLocation(x, y);
x = x + xSize;
en2Ship8.setLocation(x, y);
x = x + xSize;
en2Ship9.setLocation(x, y);
x = x + xSize;
en2Ship10.setLocation(x, y);
x = 240;
y = y + ySize;
en1Ship1.setLocation(x, y);
x = x + xSize;
en1Ship2.setLocation(x, y);
x = x + xSize;
en1Ship3.setLocation(x, y);
x = x + xSize;
en1Ship4.setLocation(x, y);
x = x + xSize;
en1Ship5.setLocation(x, y);
x = 240;
y = y + ySize;
en1Ship6.setLocation(x, y);
x = x + xSize;
en1Ship7.setLocation(x, y);
x = x + xSize;
en1Ship8.setLocation(x, y);
x = x + xSize;
en1Ship9.setLocation(x, y);
x = x + xSize;
en1Ship10.setLocation(x, y);
x = 136;
y = 450;
xSize = 98;
barrier1.setLocation(x, y);
x = x + xSize + gapSize;
barrier2.setLocation(x, y);
x = x + xSize + gapSize;
barrier3.setLocation(x, y);
x = x + xSize + gapSize;
barrier4.setLocation(x, y);
//x = x + xSize + gapSize;
player.setLocation(400 - 32, 540);
}
I have attempted to put the JLabels in an array like so:
public class SpaceInvaders extends javax.swing.JFrame {
public SpaceInvaders() {
this.enemyArray = new JLabel[]{en3Ship1, en3Ship2, en3Ship3, en3Ship4, en3Ship5, en2Ship1, en2Ship2, en2Ship3, en2Ship4, en2Ship5, en2Ship6, en2Ship7, en2Ship8, en2Ship9, en2Ship10, en1Ship1, en1Ship2, en1Ship3, en1Ship4, en1Ship5, en1Ship6, en1Ship7, en1Ship8, en1Ship9, en1Ship10};
initComponents();
//barrier1.setIcon(barrier2);
//enemyRemover.start();
}
JLabel[] enemyArray;
}
There is obviously tonnes of code generated by the IDE that I'm not including here. When I attempt to, say, change the location of the first JLabel in that array the application crashes with the following exception:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.swing.JLabel.setLocation(int, int)" because "this.enemyArray[0]" is null
As I am completely new to this I'm probably not doing a good job of describing the issue either but what I essentially want is to manipulate all of these 25 JLabels that I use as sprites in bulk.
Thanks for any help and Cheers!