0

I have a problem in NetBeans with Command-Line Arguments, when run this code it says

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 

Note I put an argument in command line for NetBeans

public class NewEmpty1
{
  public static void main(String arg[]){
   System.out.println(arg[0]);
  }
}

What is wrong ?

2
  • i guess you are compiling and running the program by invoking another class main method... Commented Feb 9, 2013 at 8:55
  • No , I am sure from compiling and running by invking class main method which I have put an argument to it Commented Feb 9, 2013 at 9:01

4 Answers 4

1

goto Project-Property-Run here you will see the option main class arguments

now make sure you are accessing the correct main class....after this option you have button to browse the class path. select it and then select the arguments finally you should be able to run the program...cheers!

Ashish

Sign up to request clarification or add additional context in comments.

Comments

0

You have not passed any arguments..

And if you have passed arguments then it may be because you are invoking another class main method in the same package


the best way would be to iterate..

for(string s:arg)
     System.out.println(s);

or

for(int i=0;i<arg.length();i++)
    System.out.println(arg[i]);

3 Comments

but I put an argument in project properties
@user1841718 do your program have multiple main methods in different class..it may be because you are executing through a different class main method..i.e you may be passing arguments to another main method
yes , it have many main method but I am sure from selecting the class main method which I have put an argument to it
0
subscript the string beyond its index is undefined.

this is your case. args[] is empty.

check this How to pass cmd line argument

Comments

0
public class NewMain {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int argslen=args.length;
        int argsValue[] = new int[argslen];
        for (String i:args) {
           int d = 0;
           argsValue[d]=Integer.parseInt(i);
           System.out.print(argsValue[d]+"\t"+"\n");
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.