Home > Software design >  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at demo.Lookup.main
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at demo.Lookup.main

Time:04-15

package demo;
import java.net.*;

public class Lookup {
    private InetAddress inet = null;
    public void resolve(String host){
        try{
            inet = InetAddress.getByName(host);
            System.out.println("Host name :"  inet.getHostName());
            System.out.println("IP Address:"  inet.getHostAddress());
        }
        catch(UnknownHostException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        Lookup lookup = new Lookup();
        lookup.resolve(args[0]);
    }
}

And I got:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at demo.Lookup.main(Lookup.java:19)

Thanks

CodePudding user response:

the "args" from the main method is passing in by parameter

public static void main(String[] args)

Assuming that you are compiling the codes using command line:

javac demo\Lookup.java (I am using windows here) 

you can pass in the parameter when you execute the codes using command line

java demo.Lookup <lookup ip address here>

if you are using IDE such as Eclipse, you can configure your "Run Configuration" to pass in IP address in the "Program Arguments"

eclipse configuration

CodePudding user response:

No args are passed into your args parameter wherever you are calling this function. So when you try to get the first value, there is nothing there.

  • Related