Results 1 to 7 of 7
Thread: Help with error!!
- 02-10-2010, 06:45 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
Help with error!!
I am using openjdk on Ubuntu and I am trying to wright the following program:
public class use
{
public static void main(String[] args)
{
System.out.print ("hi");
System.out.println(args[0]);
System.out.println("gal");
}
}
I got the program out of a handbook, but I constantly get the following error:
hiExeption in thread "main" java.lang.ArrayIndexOutOfBoundsExeption:0 at use.main(use.java:11)
- 02-10-2010, 06:54 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Ok, first of all, naming convention states that class names should begin with uppercase, just a nitpick, but it's good to get the right habits right off the bat. Now, your error pops up, because of this line:
args stores command line parameters used when you launch the program, so for instance, if you ran a program like this:Java Code:System.out.println(args[0]);
java MyProg one two three
values in the args array would be as follows:
args[0] = "one";
args[1] = "two";
args[2] = "three";
You ran your program without any parameters, and so the args array was not initialised, and you tried to print out an undeclared value. To avoid this kind of problem, do this:
Now, if you run this program without a parameter (java Use), the output would be the usage, then the program quits, and doesn't terminate with an exception.Java Code:public class Use { //note the uppercase public static void main(String[] args) { if(args.length == 0) { System.out.println("Usage: java Use yourname"); System.exit(1); //nonzero exit values mean program terminated because of error } System.out.println("hi "+args[0]+" gal"); } }
- 02-10-2010, 07:01 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
If I use the program it always exits, how do I get the args.length longer so that it doesn't exit.
- 02-10-2010, 07:09 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 02-10-2010, 07:18 PM #5
Member
- Join Date
- Feb 2010
- Posts
- 5
- Rep Power
- 0
sorry I don't understand? :confused:
- 02-10-2010, 07:21 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
My post contains an example. Read it, don't just copy and paste the code.
- 02-11-2010, 07:48 AM #7
Senior Member
- Join Date
- Mar 2009
- Location
- USA
- Posts
- 127
- Rep Power
- 0
run your code with line
System.out.println(args.length);
instead of
System.out.println(args[0]);
if you do that, it will show the array length 0(means its empty). but u r trying to print the 0th element(1st element in array) which is not there. thats why its exception.ou
So, inorder to print the 1st element in array, you need to insert into array. So you do that
from command line.
look at josah's suggestion.
Similar Threads
-
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10-26-2009, 04:38 PM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM -
error 530 error authentication required
By rgale in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-12-2008, 04:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks