wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Strange Error [java.lang.ArrayIndexOutOfBoundsException] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strange Error [java.lang.ArrayIndexOutOfBoundsException]" Watch "Strange Error [java.lang.ArrayIndexOutOfBoundsException]" New topic
Author

Strange Error [java.lang.ArrayIndexOutOfBoundsException]

Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
Hello Java Experts,

1. I have three classes
- Launcher........... (which launches 1 Parallel_Thread thread and 1 Shared Object)
- Parallel_thread.. (which talks twice to the shared object and then terminates)
- SharedObject.....(which runs until it is shut-down by the launcher class)

2. All Classes "javac" without any errors!

3. This is the Launcher Code

When running the launcher.class via java launcher<enter> from the commandline, this is the error that appears
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Launcher.main(Launcher.java:57)

Now my questions about this message?
1. How is it possible to get an ArrayIndexOutOfBoundsException error?
I do not enter any commandline arguments when starting this class, so the Args[] array is not being used.

2. How is it possible to get an error in LINE 57, while my Launcher.java sourcecode is only 33 lines???
This is really weird to me :~}

Thanks for sharing your insights with me.

Ronald
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11441
    
  16

is that the full error message?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
A couple of suggestions/questions:

1. When you launch by: java launcher<enter>, I assume you mean java Launcher<enter>. Or could it be that you are not running the code that you think you are?

2. Try putting in more logging to narrow down the statement that causes the error. Does the "Launcher - Start" appear in the output?

3. How are your two threads communicating?

4. You don't show the code for SharedObject and Parallel thread. What do they do?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

The error message you posted was thrown from line 57 of the Launcher class, in the main() method. But the code you posted for the Launcher class doesn't even have 57 lines. So the code you posted isn't the code which threw that exception. Fix that problem first.
Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
The story continues, At first here are all sourcecodes

1. Launcher.java

2. Parallel_thread

3. Shared Object
4. Some diagnostic background info:
ronald@camelion:~/Desktop/java_exercisis/threads/a/example0_thread_templates> java -version
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode)


5. Full error message after:
- removing all previous classfiles in the directory
- recompiling from the command-line with java /absolute/PATH/to/the/.java file

ronald@camelion:~/Desktop/java_exercisis/threads/a/example0_thread_templates> java Launcher
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Launcher.main(Launcher.java:57)
ronald@camelion:~/Desktop/java_exercisis/threads/a/example0_thread_templates>



I am wondering if you get the same results (error), when compiling these source-codes?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
I concur with Paul's post: you're not running the code that you think you're running. When I compile and run the code I get this output:


Ping & DNS - my free Android networking tools app
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

i agree with Ulf ... even i ran it and it is running just fine


http://plainoldjavaobject.blogspot.in
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Perhaps you are using older versions of your class files. Try throwing your class files away, then recompiling your source code.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
Ulf Dittmer wrote:I concur with Paul's post: you're not running the code that you think you're running. When I compile and run the code I get this output:

THAT is the result i was searching for too Ulf! Thanks for the feedback and from running it on your own system.


UPDATE for all
1. I have searched my entire harddisk(s) and found NO second copy of Launcher.java and Launcher.class (not even in hidden directories, "recycle bin" or "Trash/files" directories.

2. I have removed all .class files from the working directory and recompiled the 3 sourcecodes again -> Same error!

3. Finally i found a "solution:"
- I renamed - the Launcher class to Ronald class (in line 1 of the Launcher.java file)
- I renamed - the file Launcher.java to Ronald.java
- I recompiled this Ronald.java example ..... en voila... error gone :-) so this problem is solved

4. It still keeps wondering/bugging me how it was possible that this error occurred? An error inline 57 of a 34 line sourecode.

Questions
A - Is it possible that there is was another Launcher.java, Lancher.class file on my system -> No rule out that one by a disk search.
B - Does the JAVAC compiler itself store any old compilation results (class files) for possible re-use? or faster re-compilation
C - When I enter Javac <filename.java> HOW does JAVAC find the filename.java:
----- Does JAVAC search in the current directory first?
----- Does JAVAC search along the PATH of the computer it runs on?
----- Option 3?

Thank you all for your valuable feedback and explanation on this "error".
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3838

Ronald Vermeij wrote:B - Does the JAVAC compiler itself store any old compilation results (class files) for possible re-use? or faster re-compilation

Only the last compiled one unless you moved it after the compilation or use -d with "javac" to compile it to another location.
C - When I enter Javac <filename.java> HOW does JAVAC find the filename.java:

If it's only the file name then it assumes it is from the current directory, or in the full path to the source file if given.
Does JAVAC search along the PATH of the computer it runs on?

PATH is for a different purpose, to make the executable files available to the command prompt.

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
@Rob: Look to previous ans first reply..
Before re-compiling, I have (always) removed all old class file in same folder each time.

@Vijitha: Thanks for your answers on the Javac compiler.

Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Perhaps the Launcher.class file is in a jar file in your classpath.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange Error [java.lang.ArrayIndexOutOfBoundsException]