• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strange Error [java.lang.ArrayIndexOutOfBoundsException]

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is that the full error message?
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree with Ulf ... even i ran it and it is running just fine
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps you are using older versions of your class files. Try throwing your class files away, then recompiling your source code.
 
Ronald Vermeij
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ronald Vermeij
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps the Launcher.class file is in a jar file in your classpath.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic