0

Can't figure out why I am getting this exception, Console returns this message:

public static void main(String [] args){
JFrame b = new JFrame("Lotus");
Container pieces = new Container();
JLabel[] labelsP1 = new JLabel[10];
JLabel[] labelsP2 = new JLabel[10];

for(int i = 0 ; i < labelsP1.length ; i++){
    labelsP1[i] = new JLabel(B1); 
    for(int j = 0 ; j < labelsP2.length ; j++){
        labelsP2[j] = new JLabel(B2); 
    }
    for (JLabel label : labelsP1) {
        pieces.add(label);
    }

    Container c = b.getContentPane();
    c.setLayout(new GridLayout(13,3)); 
    c.add(pieces);
}
3
  • 2
    I've reformatted your code - it would be really helpful if you could format your code nicely when posting questions in future. Please read tinyurl.com/so-hints. Also, you haven't told us anything about pieces. Commented Apr 5, 2013 at 18:35
  • Where is declaration of pieces variable? Commented Apr 5, 2013 at 18:36
  • I guess pieces is null. Commented Apr 5, 2013 at 18:38

3 Answers 3

3

your 3rd inner loop is iterating throw a null array

 for (JLabel label : labelsP1) {
        pieces.add(label);
    }

//

for(int i = 0 ; i < labelsP1.length ; i++)
{
    labelsP1[i] = new JLabel(B1); 
    for(int j = 0 ; j < labelsP2.length ; j++)
    {
        labelsP2[j] = new JLabel(B2); 
    }
    for (JLabel label : labelsP1)   // null only labelsP1[0] is initialized
    {
        pieces.add(label);        
    }

    Container c = b.getContentPane();
    c.setLayout(new GridLayout(13,3)); 
    c.add(pieces);
}
Sign up to request clarification or add additional context in comments.

2 Comments

As labelsP1 is filled upto i. Also the nesting of for i and for j suggests one new JLabel[] should happen inside the first for i.
check Deadron comment, it looks like your code is missing a bracket.
2

For starters it does not look like you are showing us all the code which things difficult. Secondly, is it the intent that your first for loop is containing all of the code after it? It seems likely you have a bracket misplacement issue. I would guess you need a closing brace.

for(int i = 0 ; i < labelsP1.length ; i++){
        labelsP1[i] = new JLabel(B1); 
        for(int j = 0 ; j < labelsP2.length ; j++){
            labelsP2[j] = new JLabel(B2); 
    }

to

for(int i = 0 ; i < labelsP1.length ; i++) {
    labelsP1[i] = new JLabel(B1);
}

for(int j = 0 ; j < labelsP2.length ; j++) {
    labelsP2[j] = new JLabel(B2); 
}

Comments

0

In thie code below :

for(int i = 0 ; i < labelsP1.length ; i++){
labelsP1[i] = new JLabel(B1); 
for(int j = 0 ; j < labelsP2.length ; j++){
    labelsP2[j] = new JLabel(B2); 
}
for (JLabel label : labelsP1) {
    pieces.add(label);
}

You are adding JLabels to the array labelsP1 one by one and also iterating over the array. But you are trying to access all the elements from it which does not exist.

I would suggest you to remove nested for loops to get proper behaviour and avoid NullPointerException.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.