Java错误“线程中的异常”主要" java.lang.ArrayIndexOutOfBoundsException" [英] Java Error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException"

查看:98
本文介绍了Java错误“线程中的异常”主要" java.lang.ArrayIndexOutOfBoundsException"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是本书中的简单代码,它在netbeans和通过命令提示符运行的编译版本(.class)版本中生成错误消息。

Here is this simple code from my book it produces error message in netbeans and in compile version (.class) version running through Command prompt.

错误消息

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at intocm.Intocm.main(Intocm.java:17)

将英寸转换为厘米的简单程序。

Simple Program to convert inches to centimeter.

package intocm;

public class Intocm {

    public static void main(String[] args) {
        // TODO code application logic here
        double inches;
        inches = Double.valueOf(args[0]).doubleValue();
        double cm;
        cm = inches * 2.54;
        System.out.println(cm + "Centimeters");
    }
}

导致错误的行是

inches = Double.valueOf(args[0]).doubleValue();

我不知道为什么这个数组args导致此错误请帮助我理解这一点。

I don't know why this array "args" causing this error please help me in understanding this.

谢谢。

推荐答案

您没有传递命令行参数。 args [0] 期待命令行参数。

You are not passing command line arguments. args[0] is expecting a command line argument.

如果从命令行运行它,请尝试:

IF you are running it from command line try this:

java Intocm 12.0

在日食中

Run---> Run Configuration--->
                            Arguments Tab--->
                                            give program arguments-->
                                                                  apply---> run

这篇关于Java错误“线程中的异常”主要" java.lang.ArrayIndexOutOfBoundsException"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆