ArrayIndexOutOfBoundsException中的错误:0

时间:2013-07-10 11:55:02

标签: java arrays runtime-error indexoutofboundsexception

我在java.lang.ArrayIndexOutOfBoundsException: 0收到错误,那么如何避免此错误?

package javaapplication1;

import java.net.*;
import java.io.*;

public class Url {

    public static void main(String[] args) {
        try {
            URL url = new URL(args[0]);
            System.out.println("URL is " + url.toString());
            System.out.println("protocol is "
                    + url.getProtocol());
            System.out.println("authority is "
                    + url.getAuthority());
            System.out.println("file name is " + url.getFile());
            System.out.println("host is " + url.getHost());
            System.out.println("path is " + url.getPath());
            System.out.println("port is " + url.getPort());
            System.out.println("default port is "
                    + url.getDefaultPort());
            System.out.println("query is " + url.getQuery());
            System.out.println("ref is " + url.getRef());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ERROR:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at javaapplication1.Url.main(Url.java:10) Java Result: 1

5 个答案:

答案 0 :(得分:2)

发生此错误是因为传递给java程序的参数数组没有元素或没有传递给程序,为避免在使用其值之前进行检查

if(args.length > 0) {
    // do your task
}

那么你的amin将是

public static void main(String[] args) {
    if(args.length > 0) {
        try {
            URL url = new URL(args[0]);
            System.out.println("URL is " + url.toString());
            System.out.println("protocol is "
                    + url.getProtocol());
            System.out.println("authority is "
                    + url.getAuthority());
            System.out.println("file name is " + url.getFile());
            System.out.println("host is " + url.getHost());
            System.out.println("path is " + url.getPath());
            System.out.println("port is " + url.getPort());
            System.out.println("default port is "
                    + url.getDefaultPort());
            System.out.println("query is " + url.getQuery());
            System.out.println("ref is " + url.getRef());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:1)

为了避免错误,您必须在访问此数组的索引0之前检查数组 args 的长度。 在大多数情况下,最好打印一些使用消息并使用某个错误代码退出。

if (args.length != 1)
{
   System.err.println("Wrong number of arguments!");
   System.err.println("Usage: java javaapplication1.Url <URL>");
   int errorcode = -1; // choose an appropriate number here!
   System.exit(errorcode);
}
// now you can be sure that the args has exactly one element.

答案 2 :(得分:0)

您似乎没有将url作为命令行参数传递。

尝试运行“java Url&lt;在这里提供网址&gt;”等程序 例如java Url www.google.com

答案 3 :(得分:0)

问题是你在没有传递参数的情况下运行,因此arg [0]不存在。

我会在程序的最开头添加一个检查,以确保参数数组实际传入。

类似的东西:

if(args.length>0) {
//Rest of your code here...
}

答案 4 :(得分:0)

您必须首先检查args表是否为空。我建议使用这样的东西:

    try{
        if(args.length > 0){
            URL url = new URL(args[0]);
            System.out.println("URL is " + url.toString());
            System.out.println("protocol is " + url.getProtocol());
            System.out.println("authority is " + url.getAuthority());
            System.out.println("file name is " + url.getFile());
            System.out.println("host is " + url.getHost());
            System.out.println("path is " + url.getPath());
            System.out.println("port is " + url.getPort());
            System.out.println("default port is " + url.getDefaultPort());
            System.out.println("query is " + url.getQuery());
            System.out.println("ref is " + url.getRef());
        } 
    }
    catch (IOException e) {
        e.printStackTrace();
    }