1

我遇到了一个错误 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();
        }
    }
}

错误:

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

5 回答 5

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();
        }
    }
}
于 2013-07-10T12:03:23.990 回答
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.
于 2013-07-10T12:04:21.133 回答
0

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

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

就像是:

if(args.length>0) {
//Rest of your code here...
}
于 2013-07-10T12:04:04.130 回答
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();
    }
于 2013-07-10T12:10:50.797 回答
0

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

尝试运行类似“java Url <在此处提供网址>”之类的程序,例如java Url www.google.com

于 2013-07-10T11:56:49.997 回答