Java JLabel对象数组初始化问题

Java JLabel对象数组初始化问题,java,arrays,initialization,Java,Arrays,Initialization,因此,我将它初始化为数组对象,当我初始化单个对象时,它不会给它们一个值 /* -A java program GUI for Food to allow user to type the food */ public class Catagorygui extends JFrame implements ActionListener{ //attributes JPanel panel; JButton button; JFrame frame; JLabel item;

因此,我将它初始化为数组对象,当我初始化单个对象时,它不会给它们一个值

/* 
-A java program GUI for Food to allow user to type the food 
*/
public class Catagorygui extends JFrame implements ActionListener{

//attributes
JPanel panel;   
JButton button; 
JFrame frame;   
JLabel item;    
JTextField item1;   
JLabel cost;    
JTextField cost1;   
JLabel percentused; 
JLabel name;    
JTextField name1;   
JLabel paidupfront; 
JTextField paidupfront1;

public Appartment2 g=new Appartment2();

private static String rec=" ";

private JTextField [] upfront=new JTextField[g.getPeople().size()];//must initialize
private JTextField [] percent=new JTextField[g.getPeople().size()];
private JLabel [] usernames=new JLabel[g.getPeople().size()];//for loop that gives them values
private int a;

public Catagorygui (){
    g.addPerson("Ali");
    g.addPerson("Kacie");

    for(int i=0;i<a;i++)
    {

        usernames[i]=new JLabel("poop");//somehow not initializing it
        percent [i]=new JTextField(10);
        upfront [i]=new JTextField(10);
        percent[i].addActionListener(this);
    }
    System.out.println(g.getpeople().get(0).getName());//ali
    System.out.print(usernames[0]);//SAYS INDEX OUT OF BOUNDS, should say'poop'
    //
    a=g.getpeople().size();
/*
-用于食品的java程序GUI,允许用户键入食品
*/
公共类Catagorygui扩展JFrame实现ActionListener{
//属性
JPanel小组;
按钮;
JFrame框架;
JLabel项目;
JTextField项1;
JLabel成本;
JTextField成本1;
使用JLabel百分比;
JLabel名称;
JTextField名称1;
JLabel-paidupfront;
JTextField paidupfront1;
公共公寓2 g=新公寓2();
私有静态字符串rec=“”;
private JTextField[]upfront=new JTextField[g.getPeople().size()];//必须初始化
私有JTextField[]百分比=新JTextField[g.getPeople().size()];
private JLabel[]usernames=new JLabel[g.getPeople().size()];//用于给它们赋值的循环
私人INTA;
公共分类{
g、 addPerson(“阿里”);
g、 addPerson(“Kacie”);

for(int i=0;i
for(int i=0;i
for(int i=0;i
a=g.getpeople().size();
应该在
for
循环之前

a=g.getpeople().size();
应该在
for
循环之前

你就快到了,只需稍微移动一些变量

for循环没有执行,因为a没有初始化(或者是初始化,但在构造函数末尾…),只需在进入for循环之前移动变量a的赋值,而不是必须消除错误! a=g.getpeople().size();

例子:
公共分类GUI(){
g、 addPerson(“阿里”);
g、 addPerson(“Kacie”);
a=g.getpeople().size();//移到这里

对于(inti=0;i你就快到了,只需移动一些变量

for循环没有执行,因为a没有初始化(或者是初始化,但在构造函数末尾…),只需在进入for循环之前移动变量a的赋值,而不是必须消除错误! a=g.getpeople().size();

例子:
公共分类GUI(){
g、 addPerson(“阿里”);
g、 addPerson(“Kacie”);
a=g.getpeople().size();//移到这里

对于(int i=0;iIf
usernames[0]
将索引抛出边界,这意味着数组大小为
0
。如果
usernames[0]
将索引抛出边界,这意味着数组大小为
0
。这不提供问题的答案。若要评论或要求作者澄清,请在其帖子下方留下评论。-@GauravDave这不是要求澄清或批评。这是解释代码无法按预期工作的原因。这不正确提供问题的答案。若要批评或要求作者澄清,请在其帖子下方留下评论。-@GauravDave这不是要求澄清或批评。这是解释代码无法按预期工作的原因。
for (int i=0; i<a; i++)
public Catagorygui (){
    g.addPerson("Ali");
    g.addPerson("Kacie");
    a = g.getpeople().size();   // move up to here
    for(int i=0;i<a;i++)
    {
        usernames[i]=new JLabel("poop");//somehow not initializing it
        percent [i]=new JTextField(10);
        upfront [i]=new JTextField(10);
        percent[i].addActionListener(this);
    }
    System.out.println(g.getpeople().get(0).getName());//ali
    System.out.print(usernames[0]);//SAYS INDEX OUT OF BOUNDS, should say'poop'
        //