본문 바로가기
에러 모음 및 해결

[ERROR | Java] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at 오류

by NpsCause 2022. 9. 8.
public class Geometry {
	public static void main(String[] args) {
		System.out.println(args[0]);
	}
}

에러코드


위 코드를 컴파일하였을 때 오류가 발생하였다.

' Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at '

에러문


해결방법

위 오류는 배열을 잘못 참조했을때 나오는 오류이다.

때문에 자바 파일을 실행할 때 args에 인자를 넘겨주고 사용하면 에러가 해결된다.

 

java 파일이름.java 인자값 {인자값2} {인자값3} ...

위와 같이 파일 이름 뒤에 인자값을 넘겨주면 args배열에 순서대로 저장되게 된다.

 

ex)

java Geometry.java onw two three

댓글