Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:0_Java - Fatal编程技术网

“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:0

“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:0,java,Java,我试图通过传递主机名来获取主机地址。但在运行代码时,我遇到了一个异常: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at socketprogram_client.SocketProgram_Client.main(SocketProgram_Client.java:16) Java Result: 1 有人能告诉我为什么我会犯这个错误吗 这是我的密码: package socketpro

我试图通过传递主机名来获取主机地址。但在运行代码时,我遇到了一个异常:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at socketprogram_client.SocketProgram_Client.main(SocketProgram_Client.java:16)
Java Result: 1
有人能告诉我为什么我会犯这个错误吗

这是我的密码:

package socketprogram_client;

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

public class SocketProgram_Client 
{
    public static void main ( String args[] ) throws IOException 
    {
        String host_name = args[0];

        try
        {
            InetAddress my_ipaddr = InetAddress.getByName(host_name);
            System.out.println("Resolved to IP address: " + my_ipaddr.getHostAddress());
        }
        catch ( UnknownHostException e )
        {
            System.out.println("Could not find IP address for: " + host_name);
        }
    }
}

您可能没有向
args
传递参数,因此
args[0]
在那里可能为空

package socketprogram_client;

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

public class SocketProgram_Client 
{
    public static void main ( String args[] ) throws IOException 
    {
        if(args.length == 0){
            System.out.println("You didn't say a host name");
            System.exit(1);
        }

        String host_name = args[0];

        try
        {
            InetAddress my_ipaddr = InetAddress.getByName(host_name);
            System.out.println("Resolved to IP address: " + my_ipaddr.getHostAddress());
        }
        catch ( UnknownHostException e )
        {
            System.out.println("Could not find IP address for: " + host_name);
        }
    }
}

看来你没有传递任何论点。你应该做一些检查:

public static void main ( String args[] ) throws IOException 
{
    if(args == null || args.length == 0){
        System.out.println("You didn't pass in any arguments!");
    }
    else{
        //rest of code
    }
}
运行程序时,命令行中必须有一个参数:

java SocketProgram_Client the_argument

参数
是您正在传递的参数(
args[0]
)。

请注意,原始文件中的代码格式不清楚。当我添加格式时,很可能行号发生了变化,第16行抛出的异常不再与第16行对应。检查是否传递了任何参数。执行System.out.println(args.length)或学习调试程序。第16行在哪里(显然是发生错误的那一行)?在该行上索引的是哪个数组?阵列有多大?索引值是多少?你看到问题了吗?哇,我完全忽略了ugh@Betlista抢手货我完全忘记了
args
不能为
null
。只是习惯性地在清空之前检查null。实际上,如果从另一个方法调用
main
,则
args
可以为null。但人们通常不需要对此进行保护。这是一个很好的习惯,我仔细考虑了一下,它在命令行中不会为null,但程序员也可以调用此方法…@HotLicks,那么进行此检查没有什么害处。