英文:
Exception in thread "main" java.lang.ArrayOutOfBoundsException: Index 0 out of bounds for length 0
问题
我在大学学习,对编程还很陌生。教授正在教我们Java,但每次尝试运行她的代码时都会遇到这个错误。
这已经是我第二次遇到这个问题了,上次花了很多时间来解决它,我甚至不记得我当时是怎么做的。有人可以帮帮我吗?
英文:
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?
答案1
得分: 1
这是一个由Java提供的关于框架内使用的异常机制的出色教程。
你遇到的错误如下所示。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at Programa01.main(Programa01.java:11)
这是由一个语句引发的异常。
这里的第一行概述了异常的类型,在这种情况下是ArrayIndexOutOfBoundsException。
然后是一条消息,虽然有时会包含相关信息。
在这种情况下,它指出了以下内容。
> Index 0 out of bounds for length 0
首先,您需要确定如何定位错误。
错误的最后一行包括发生错误的行号。
在这种情况下,它是在.java文件扩展名之后的括号内标注的第11行。
at Programa01.main(Programa01.java:11)
如果您查看第11行,您将找到以下代码。
NumInt = Integer.parseInt(args[0]);
这一行中有一些语句。
首先,您有NumInt的赋值。
NumInt =
此外,您有Integer#parseInt方法的调用。
Integer.parseInt()
最后,您访问了args数组。
在这里,您的语法指定了要解析的索引0。
args[0]
由于引发的异常是ArrayIndexOutOfBoundsException,我们可以推断逻辑从最后一个语句args数组访问传播出来。
因此,args数组的索引0是不可访问的,因为args的长度为0,它不包含任何元素。
args参数是由Java虚拟机在程序启动时提供和填充的参数。
这是关于命令行参数的教程。
由于您正在使用VSCode,您可以参考以下关于添加启动配置的教程。
如果您需要有关添加值的更多帮助,请随时留下评论。
英文:
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.
答案2
得分: 0
你应该传递3个参数,就像你的代码期望的那样。目前你没有传递任何参数。由于你是通过VSCode IDE启动你的代码,尝试通过IDE添加参数:
Run -> Add Configuration...
它会打开 launch.json 文件。在指定 mainClass 的部分添加带有参数的行:
"args": ["one", "two", "three"]
考虑根据你的代码逻辑添加正确的参数:整数、双精度 和 字符串。整个 launch.json 应该是一个有效的JSON文件,因此请不要忘记逗号和参数周围的引号,即使它们不是字符串也要加上。这应该有所帮助,祝你好运。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论