1

I'm working on a simple program for school that's a simple String builder, I'm properly defining the array size, yet when I run the program, ArrayIndexOutOfBoundsException: 0 is thrown. I initialize the variables in the constructor, name and classes. name is the student's name, classes is how many classes the student has each day. The size of my arrays is defined using classes. Here is my constructor and variables.

protected String name = "";
protected int classes;
private String schedule = "";
private String[] course = new String[classes];
private String[] room = new String[classes];
private int[] Period = new int[classes];

public StringBuilderHandler(String name, int classes) {
    this.name = name;
    this.classes = classes;
}

I'm using a for loop to set what String schedule will be.

private void setClass(int index) {
    Scanner scan = new Scanner(System.in);
    class[index] = scan.nextLine();
}

private void setPeriod(int index) {
    Scanner scan = new Scanner(System.in);
    period[index] = scan.nextInt();
}

public void setRoom(int index) {
    Scanner scan = new Scanner(System.in);
    room[index] = scan.nextLine();    
}

public void buildSchedule() {
    for (int i = 0; i < classes; i++) {
        System.out.println("What is your class?");
        setClass(i);
        System.out.println("What period is this class?");
        setPeriod(i);
        System.out.println("What room is this class?");
        setRoom(i);
        schedule = schedule +"Period "+period[i]+"\t"+course[i]+"\tRoom "+room[i]+"\n";
    }
}

Any ideas?

3
  • Shouldn't class[index] be course[index] in setClass? Commented Oct 6, 2012 at 4:52
  • Don't forget to accept an answer since some good answers have already been posted. Commented Oct 6, 2012 at 5:30
  • @AVD: please read the homework tag wiki Commented Oct 6, 2012 at 9:43

4 Answers 4

3

You must have to create objects (array) inside the constructor.

private String[] course;
private String[] room;
private int[] Period;

public StringBuilderHandler(String name, int classes) {
    this.name = name;
    this.classes = classes;
    this.course = new String[classes];
    this.room = new String[classes];
    this.Period = new int[classes]; 
}
Sign up to request clarification or add additional context in comments.

4 Comments

This has made the code cleaner but there are still chances of an ArrayIndexOutOfBoundsException if a value of 0 I'd passed to the constructor for the classes argument. What would be ideal is to use an ArrayList
@bot Agree! and of course more attention is needed with arrays.
Thank you, this totally slipped my mind and it's kind of embarrassing. @bot Yeah it would, but my instructor's only permitting us to use specific methods.
Even if you use an arrays instead of an ArrayList, I feel my answer explained why you were getting the ArrayIndexOutOfBoundsException which was your question and not how you should initialize the arrays.
2

Your classes instance variable gets a default value of 0 since you have not initialized it. All your arrays are therefore getting initialized with a zero size. You are getting an exception because there is no index 0 since the arrays have zero size. You need to initialize the classes instance variable to a non zero positive value.

You may still get an ArrayIndexOutOfBoundsException if you try to insert more values into the array than its size. A quick solution would be to use an ArrayList instead of arrays.

Comments

2

you just need to define your class as:

protected String name = "";
protected int classes;
private String schedule = "";
private String[] course;
private String[] room;
private int[] Period;

public StringBuilderHandler(String name, int classes) {
    this.name = name;
    this.classes = classes;
    this.course = new String[classes];
    this.room = new String[classes];
    this.Period = new int[classes]; 
}

For sure it will work. You were using classes variable without initializing it because its initializing at run time. You just need to initialize array in constructor when classes has a value.

3 Comments

How does that solve te problem? What if I pass a 0 value to the constructor for the classes argument? Changing the code structure is not going to implicitly make the exception go away. This does not answer the question.
@bot you are right. But this issue solved by the programmer only because he have to make sure that 0 must not pass in method. These type of issues handle by the programmer.
You are missing the point. The thing with arrays us that they have a fixed size. You can't add elements beyond a particular index or you will get an ArrayIndexOutOfBoundsException. What do you do then to add more elements to an already full array? You either create a new array double its size and copy its content to this array or you simply use an ArrayList. I hope you understand why your answer doesn't solve the problem. See my answer.
0

Have you initialised that variable classes anywhere?

You've initialised all the arrays of size classes, but classes doesn't appear to be initialised.

1 Comment

In the main class I use methods in the Scanner class to initialize name and classes.

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.