Wolff
Wolff

Reputation: 1091

java.lang.ArrayIndexOutOfBoundsException: 0 error in Eclipse, for programs that work using command prompt?

Right, so I managed to get Eclipse to work (which we need to use in our exam). I'm importing and running some of my programs I had written with gedit and run with command prompt (which worked), but they don't seem to work in Eclipse.
My Hello World program did, but my others seem to throw errors regarding args?

package week1;

public class PersonalGreeting {

    public static void main(String[] args) {
        String first_name = args[0];
        String last_name = args[1];
        System.out.println("Hello " + first_name + " " + last_name);
    }
}

Error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0  
    at week1.PersonalGreeting.main(PersonalGreeting.java:6)

Any ideas how to fix this and what's causing it? Why should it work in command prompt but not Eclipse?

Upvotes: 1

Views: 6179

Answers (5)

unigeek
unigeek

Reputation: 2826

I would definitely pass it some parameters. You can do this with an eclipse run configuration. Look at the Run menu under Run Configurations. Switch to the Arguments tab and give some program arguments before running again.

EDIT: By the way, when you ask eclipse to run your program without giving it any further details, it creates a default run configuration for the program which then appears in the list of run configurations. I would consider issuing the run command and expecting some error in the output as a common usage pattern with eclipse--just easy to get the run configuration created that way, then go edit it so that it has whatever it needs to be happy as far as program arguments, environment variables, etc.

Upvotes: 2

tr4pt
tr4pt

Reputation: 317

public class PersonalGreeting {

 public static void main(String[] args) {
  String first_name = "";
  String last_name = "";
  if (args.length > 0){
       first_name = args[0];
       if (args.length > 1) last_name = args[1];
  }else{
       first_name = "parameters";
       last_name = "are missing";
  }
  System.out.println("Hello " + first_name + " " + last_name);
 }
}

try to catch errors

Upvotes: 1

Kayz
Kayz

Reputation: 667

That is, because you are not giving any method parameters. When you run your programm from console, you have to enter firstname and lastname, those are missing in eclipse, thus the IndexOutOfBoundsException.

To solve this do following: right-click your project. choose run as. go to run configurations. Enter the first name and last name as Program arguments in the argurments tab.

Upvotes: 2

Vlad Schnakovszki
Vlad Schnakovszki

Reputation: 8601

Eclipse does not pass the parameters as the command line does.

You will need to go to your run configuration (right click the project -> Run As -> Run Configurations and select your application). You would click 'Arguments' and type your arguments in the 'Program arguments' field, then click run.

Upvotes: 2

Jorge_B
Jorge_B

Reputation: 9872

It looks like the program did not receive the expected command line parameters. Have a look at your execution configuration inside Eclipse in order to pass the mandatory parameters to any command line program you wrote.

Upvotes: 1

Related Questions