error adding textboxes to panel
yet another null pointer exception, altho i cannot see why it will not allow me to add the textbox to the panel. code:
Code :
// Put the data in the panels
for(int a = 2; a < 16; a++){
if(a == 12){
// Create panel11 to hold the line seperator(s)
panel[a] = new JPanel();
panel[a].setForeground(Color.BLUE);
panel[a].setBackground(Color.WHITE);
panel[a].add(new JLabel("___________________________________________________" +
"_______________________________________________________"));
} // End IF
else if(a == 2){
panel[a] = new JPanel();
panel[a].setBackground(Color.WHITE);
panel[a].setForeground(Color.BLUE);
for(b = 0; b <= 6; b++){
panel[a].add(arrLabel[b]);
arrLabel[b].setForeground(Color.PINK);
}
}
else {
panel[a] = new JPanel();
panel[a].setBackground(Color.WHITE);
panel[a].setForeground(Color.BLUE);
for(int i = 0; i < 76; i++){
if (i % 7 == 6) { a++; }
txtBox[i] = new JFormattedTextField();
txtBox[i].setHorizontalAlignment(JFormattedTextField.RIGHT);
txtBox[i].setColumns(9);
txtBox[i].setBackground(Color.PINK);
txtBox[i].setForeground(Color.RED);
if(i == 0){txtBox[0].setBackground(colTableB); txtBox[0].setEditable(false);}
if(i == 6){txtBox[6].setBackground(colTableA); txtBox[6].setEditable(false);}
THIS IS WHAT IS GIVING THE PROBLEM:
panel[a].add(txtBox[i]);
}
} // End ELSE
}
Compiler Error:
Code :
Exception in thread "main" java.lang.NullPointerException
at frmClient.panelSetup(frmClient.java:127)
at frmClient.<init>(frmClient.java:65)
at frmClient.main(frmClient.java:46)
So the error is leading to the line that i have made bold. without the if statement in the loop it seems to work.
the program is simple. 75 textboxes 7 in each panel. that is all that needs knowing for the mean time. So simply wondering what is the compiler saying about this line. Why can i not add the textbox to the panel with the if statement, Unless its because im declaring the new panel above the if statement and it doesnt know which panel im trying to add to as it panel[i] would have not been created yet. if im correct let me know:D although im going to try the solution and see if it solves the problem. if so ill post saying solved
Re: error adding textboxes to panel
ok yes im sorry for the trouble of reading this. i was correct. i guess i should have tried it earlier. I have fixed the problem by simply removing the code to declare a new panel from inside the for loop. to a new method which i had declared all 16 panels. and then ran the method before the for loop was executed. Simple fix.