Error in Java array ArrayIndexOutOfBoundsException

2

Good, I am learning to program in Java and when executing the code:

package useargumeny;

public class UseArgumeny {
    public static void main(String[] args) {
        System.out.print("Hi, ");
        System.out.print(args[0]);
        System.out.print(".how are you");
    }
}

The compiler throws me the following

  

error: package useargumeny: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0   Hi, at useargumeny.UseArgumeny.main (UseArgumeny.java:7)   C: \ Users \ probook \ AppData \ Local \ NetBeans \ Cache \ 8.1 \ executor-snippets \ run.xml: 53: Java returned: 1   BUILD FAILED (total time: 0 seconds)

    
asked by David Castro P 28.01.2016 в 23:19
source

3 answers

3

You have here args[0] something called Arrangement which is more or minus a sequence of data of the same type accessible by an index. And aside args is a special arrangement of programs in java where you can pass parameters from another place.

What happens here is that when you execute your code from an IDE (netbeans in your case) No argument is sent to your program and that is why it shows the exception ArrayIndexOutOfBoundsException that indicates that you are asking for an item not contained in your arrangement

An analogy of the real world would be:

  

Your mother sends you to buy things at the super, when you return home she asks for the change, but you spent all the money, so you say I can not give it to you instead of just giving you the change.

To run your program open a console, and place yourself in your folder where your .class file is generated and type

java UseArgumeny David

This will half-run your program, since it now receives an argument, and shows it without problem.

To solve the case that you are not sent arguments, use a statement if

System.out.print("Hi, ");
if(args.length > 0){
    System.out.print(args[0]);
}
System.out.print(".how are you");
    
answered by 28.01.2016 / 23:31
source
1

Simply the args array has no value to get args[0] :

 public static void main(String[] args) {
            ...
            System.out.print(args[0]);
            ...           
        } 

You would have to validate this case.

 public static void main(String[] args) {
            ...
            if (args.length > 0) {
            System.out.print(args[0]);
            }else{
            System.out.print("args esta vació!");
            } 
            ...           
        } 
    
answered by 28.01.2016 в 23:28
1

It's because you're not validating if the array args has elements. Your program No will fail if you add a command line argument, when you execute it:

$ java UseArgumeny.class rnrneverdies
Hi, rnrneverdies.how are you

To solve it you must validate if there are arguments using args.length in some way and if there are not, for example, you can give another message or whatever you need.

package useargumeny;

public class UseArgumeny {
    public static void main(String[] args) {
        if (args.length > 0) {
            System.out.print("Hola, ");
            System.out.print(args[0]);
            System.out.print(" ¿Como estas?");
        } else {
            System.out.print("Quien eres tu?");
        } 
    }
}
    
answered by 28.01.2016 в 23:25