Dan
Dan

Reputation: 639

Array Runtime Error

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?

Upvotes: 1

Views: 2616

Answers (4)

MouseCrasher
MouseCrasher

Reputation: 473

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.

Upvotes: 2

Chetan Kinger
Chetan Kinger

Reputation: 15212

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.

Upvotes: 2

KV Prajapati
KV Prajapati

Reputation: 94653

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]; 
}

Upvotes: 3

dwjohnston
dwjohnston

Reputation: 11900

Have you initialised that variable classes anywhere?

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

Upvotes: 0

Related Questions