-1

enter image description here

I'm in college and new to coding. The professor is teaching us Java, but I keep encountering this error whenever I try running her code.

This is the second time I encounter this problem, and I spent so much time trying to solve it last time I don't even remember what I did. Can someone help me please?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Welcome to Stack Overflow! Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what is the issue. See the [How to Ask page](https://stackoverflow.com/help/how-to-ask) for help clarifying this question. Include the desired behavior, a specific problem or error and the shortest code necessary to reproduce the issue. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – aled May 31 '23 at 20:23
  • An array of length `0` contains nothing. – Elliott Frisch May 31 '23 at 20:23
  • 1
    You are not running this correctly. It expects three command line arguments, and you are providing none. – Elliott Frisch May 31 '23 at 20:28
  • ohhh right!! idk how to do that on VSCode tho.. but thanks for responding :) – lovely trash May 31 '23 at 21:35
  • Welcome to StackOverflow. If an answer solves your problem you could click '✔' to mark it as an acceptable answer. If it helps you give it an upvote. If the answer is offtopic or doesn’t help at all, downvote or add a comment. See also [stackoverflow.com/help/why-vote](https://stackoverflow.com/help/why-vote) – JialeDu Jun 09 '23 at 01:01

2 Answers2

1

Here is a great tutorial by Java, on the exception mechanism used within the framework.
What Is an Exception? (The Java™ Tutorials > Essential Java Classes > Exceptions)

The error you're encountering states the following.

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

This is caused by an Exception which was thrown by one of the statements.

The first line here, outlines the type of Exception, in this case an ArrayIndexOutOfBoundsException.
Which is followed by a message—optional, although will sometimes includes relative information.

In this case it states the following.

Index 0 out of bounds for length 0

Firstly, you'll need to determine how to locate the error.

The last line in the error includes the line-number of which the error occurred.
In this case it is line 11, as denoted within the parentheses, after the .java file-extension.

at Programa01.main(Programa01.java:11)

If you look at line 11, you'll find the following code.

NumInt = Integer.parseInt(args[0]);

There are a few statements in this line.

Firstly, you have the NumInt assignment.

NumInt = 

Additionally, you have the Integer#parseInt method call.

Integer.parseInt()

And, finally, you have the args array access.
In which your syntax is specifying a resolve to index 0.

args[0]

Since the Exception that was thrown is an ArrayIndexOutOfBoundsException, we can presume that the logic propagates from this final statement, the args array access.

Thus, index 0 of the args array is inaccessible, since the length of args is 0—it contains no elements.

The args parameter is an argument which is supplied and populated by the Java Virtual Machine upon the start of your program.

Here is a tutorial on Command-Line Arguments.
Command-Line Arguments (The Java™ Tutorials > Essential Java Classes > The Platform Environment).

Since you're using VSCode, you can refer to the following tutorial on adding Launch Configurations.
VisualStudio.com – Debugging in Visual Studio Code.

If you need more assistance on adding the values, feel free to leave a comment.

Reilas
  • 3,297
  • 2
  • 4
  • 17
0

You should pass 3 arguments as your code expects them. Currently you're passing none. Since you're launching your code via VSCode IDE, try to add arguments via IDE:

Run -> Add Configuration...

It will open launch.json file. Add the line with arguments to the section where the mainClass specified:

"args": ["one","two","three"]

Consider adding the proper arguments as per your code's logic: integer, double & string. Whole launch.json should be a valid JSON file, thus pls don't forget about commas and quotes around the arguments even if they are not strings. It should help, good luck.

grey
  • 31
  • 1
  • 5