0

So, I initialized it as an array object, and when I go to initialize the individual objects,it won't give them a value

/* 
-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();
1
  • If usernames[0] throws index out of bounds, that means the array size is 0. Commented Apr 16, 2016 at 15:09

3 Answers 3

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

Yes, keep iterating while i < a, but what's a? Where did you initialize it? Any int primitive type has a default value of 0 in Java. so 0 < 0 is always false and no iterations are done.

Sign up to request clarification or add additional context in comments.

Comments

0

a=g.getpeople().size(); should be before the for loop

2 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
@GauravDave It is not request for clarification or criticism. It is explanation why the code doesn't work as expected.
0

You are almost there, just a move a little some variables...

the for loop is not executed because a is not initialized (or it is, but at the end of the constructor...) just move the assign of the variable a before you go in the for loop and not the error must be gone! a=g.getpeople().size();

Example:

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'
        //
   

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.