-2

I keep an error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0" even though i've referenced this array bounds in the method.

`public class USCrimeLibrary  

 public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    USCrimeObject crimeObject = new USCrimeObject(args[0]); `

and the reference object:

`public class USCrimeObject {
private Crime[] crimes;
String fileName = "/Users/jpl/Developer/Java/CMIS141/WK8/Crime.csv"; 
public USCrimeObject(String fileName) {
this.crimes = new Crime[20];
readFile(fileName);
}`
Carefulle
  • 11
  • 1
  • 2
  • 5
  • 1
    You have to pass the arguments in order to use one. – Aniket Sahrawat Mar 31 '19 at 05:58
  • How do you run the code? – BlackPearl Mar 31 '19 at 06:04
  • I use MS visual Studio Code. output to terminal. I tried to output to debugger but error: Error: LinkageError occurred while loading main class USCrimeLibrary java.lang.UnsupportedClassVersionError: USCrimeLibrary (class file version 55.65535) was compiled with preview features that are unsupported. This version of the Java Runtime only recognizes preview features for class file version 56.65535 – Carefulle Mar 31 '19 at 06:07

1 Answers1

0

First of all you have to pass an argument while running the program.

But from your code I think a slight change in your code can make it executable without passing argument while running the program.

Change constructor USCrimeObject to

public USCrimeObject() {
this.crimes = new Crime[20];
readFile(fileName);
}

and from main class create USCrimeObject without argument

USCrimeObject crimeObject = new USCrimeObject(); 
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24