我做了一個簡單的函數:NullPointerExecption
public class AjouterBouton {
public AjouterBouton(int nombre) {
JButton[] buttons = new JButton[nombre];
for(int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton();
buttons[i].setBackground(Color.LIGHT_GRAY);
JPanel pan = Main.grille;
pan.add(buttons[i]);
}
}
}
「格柵」 在我的主類的簡單的JPanel:
JPanel grille = new JPanel();
int ligne = 6;
int colone = 5;
grille.setBounds(6, 117, 980, 314);
grille.setLayout(new GridLayout(ligne,colone));
最後,我加入到這個主類:
new AjouterBouton(72);
但是,我得到一個JavaNullPointerException
。
PS:我想這一點,它的工作原理:
final JButton[] buttons = new JButton[72];
for(int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton();
buttons[i].setBackground(Color.LIGHT_GRAY);
grille.add(buttons[i]);
}
但我想用方法來添加我的按鈕。我是初學者;有人能幫我嗎?
總是與你的問題,這表明其中拋出錯誤的行添加堆棧跟蹤。 –
請擺脫「卡住2小時」位。這怎麼幫助我們找出你的問題?它與問題有什麼關係 - 沒有。問這樣的問題的關鍵在於包含與問題有關的相關信息,例如堆棧跟蹤和導致該問題的行,並排除不相關的位,這些只會分散注意力。 –
@HovercraftFullOfEels和我們知道的標籤是'JAVA' – nachokk