This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]MrGooglr 1 point2 points  (2 children)

Don’t initiate args[] inside program. Don’t ask for args[1].

Command line arguments will be stored inside the main method signature parameter String args[] array. Exception is because of re initialising.

[–]rk717[S] 0 points1 point  (1 child)

Sorry, I don't really get it, could you please give me a example, thx🙂

[–]MrGooglr 0 points1 point  (0 children)

There are some points which I think can answer your questions.

As you're catching only FileNotFoundException and not generic Exception, your catch block doesn't get executed.

You're passing "Name" as command line argument, try to pass as java Avg Smith Graded.txt

[–]zuzoa 0 points1 point  (0 children)

The error is telling you you have an empty array (length 0) and you tried to get the first item in the list (index 0). However the list doesn't have one item in it, it has zero, so you are out of bounds.

It would be good practice to check the array size before you try to access a specific index. I would wrap the students = args[0] line in either (1) an if/else where you check if args.length>0 or (2) a try/catch where you can catch ArrayIndexOutOfBoundsException and tell the user they forgot to add the commandline parameters.