Results 1 to 3 of 3
  1. #1
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default ArrayIndexOutOfBoundsException error

    Java Code:
    public class Test{
    	
    	public static void main (String[] args){
                    //checkwhether first command-line argument is "Something"		
    		if(args[0] == "Something")
    		{
    			//do something			
    		}
    }
    }
    I get the error ArrayIndexOutOfBoundsException:0, something I find strange. String[] args accepts string arrays, so I am just checking whether the first string equals "Something" in the command line but every time I run, I get this error, and I can't figure out what is the problem?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default Re: ArrayIndexOutOfBoundsException error

    If there is no first command line argument there is no args[0] String. b.t.w. don't compare Strings for equality with the == operator, use the String.equals( ... ) method instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: ArrayIndexOutOfBoundsException error

    Thanks, yeah you are right. Such a silly mistake. Also using == wasn't right apparently, only after I used equals did it work.

Similar Threads

  1. Replies: 4
    Last Post: 10-02-2011, 08:29 PM
  2. Replies: 1
    Last Post: 09-27-2011, 06:47 PM
  3. Replies: 11
    Last Post: 02-22-2011, 05:23 PM
  4. Replies: 1
    Last Post: 07-31-2007, 11:47 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •