Java定义控件数组

private JLabel[] lblNumber;
lblNumber = new JLabel[7];
lblNumber[n] = new JLabel();//n可以是变量也可以是常量
lblNumber[n].setBounds(70, 10, 60, 20);

在定义中没有给数组设置大小,必须加上第二行 “lblNumber = new JLabel[7];”

否则会出现错误信息:Exception in thread "main" java.lang.NullPointerException: Cannot store to object array because "this.lblNumber" is null

提示说数组为空。

字符串数组和整型数组等等也一样

private String[] CNumber;
CNumber = new String[7];
CNumber[0] = txaNumber[0].getText();

或者在定义里面声明

private String[] Q= new String[100000];

 

posted @ 2022-09-12 16:44  adrow  阅读(96)  评论(0)    收藏  举报