0

I'm writing a simple Java code and I'm getting this error after entering the first input:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at university.GetStudentSpect(university.java:26)
at university.main(university.java:11)

Code:

import java.util.Scanner;
public class university {
    public static Scanner Reader = new Scanner(System.in);
    public static int n;
    public static int m=0;
    public static int l;
    public static StringBuilder SConverter = new StringBuilder();
    public static void main(String[] args) {
        GetStudentsNumber();
        GetStudentSpect();
    }

    public static void GetStudentsNumber() {
        System.out.println("enter the number of students");
        n = Reader.nextInt();
    }
    public static String [][] StudentSpect = new String [n][2];

    public static void GetStudentSpect() {
        for (int i=0;i<n;i++) {
            System.out.println("enter the name of the student");
            StudentSpect[i][0] = SConverter.append(Reader.nextInt()).toString();
            System.out.println("enter the id of the student");
            StudentSpect[i][1] = SConverter.append(Reader.nextInt()).toString();
            System.out.println("enter the number of courses of the student");
            l = Reader.nextInt();
            m += l;
            StudentSpect[i][2] = SConverter.append(l).toString();
        }
    }
}
karel
  • 5,489
  • 46
  • 45
  • 50

2 Answers2

1

Static code gets executed when the class is first loaded. That means, you initialize StudentSpec before your main method runs. That in turn means that n is not assigned a value yet, so it defaults to 0. Because of that, StudentSpec is an array of dimensions zero by two. (Note that it does not matter whether you put the code to initialize StudentSpec with all the other variables or later in the class, all static stuff gets initialized first.)

Then your code in main runs, calling GetStudentsNumber, which sets n, but does not initialize StudentSpec (again). Then GetStudentSpect runs, and as soon as you try to access StudentSpec, your program crashes, as it's an array with zero elements.

To fix this, initialize StudentSpec in GetStudentsNumber after reading n, i.e., move the code from the static initializer to this method.

Robert
  • 7,394
  • 40
  • 45
  • 64
  • I tried to do that at the beginning but it gave this error:illegal modifier for parameter StudentSpect;only final is permitted – amirmohammad naijian Mar 02 '19 at 08:10
  • I fixed this error too according to : https://stackoverflow.com/questions/29086903/how-to-create-public-array-accessible-by-all-methods-but-have-user-input-determ .it was really helpful thank you ❤❤❤ – amirmohammad naijian Mar 02 '19 at 08:28
0

Array Index start with 0 and you have given size 2 means it can only have positions 0 and 1 not 2.

 public static String [][] StudentSpect = new String [n][2];
```
And you are accessing array position of 2 over here.
```
 StudentSpect[i][2] = SConverter.append(l).toString();
```
So make this Change

public static String [][] StudentSpect = new String [n][3];