Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "ArrayIndexOutOfBoundsException: null" with NO stack trace

In our log files we find the following:

[2012-09-24 00:09:32.590 +0000UTC] ERROR host server1 [] [] somepackage.someclass [] [Unknown] [V3rAqPaDvvAAAAExEXhdWGyh] [pjsQwTGHzxcAAAE5j4YdGvWV] "ThreadName"  Some error happened:  java.lang.ArrayIndexOutOfBoundsException: null 

There is only this single line, and NO exception stack trace.

The try block in which this exception happens is executing dynamically-generated Java byte-code which was created using javassist.

I am wondering about two things:

  1. The java.lang.ArrayIndexOutOfBoundsException: null
  2. The missing stack-trace, despite calling the log hook using logger.error("message", theException) inside the catch block, which ordinarily would lead to a full stack-trace being printed in the log-file.

My Questions:

  1. What kind of code can cause a logging output "java.lang.ArrayIndexOutOfBoundsException: null". I try to reproduce this with a test program with no luck. I always get something like "java.lang.ArrayIndexOutOfBoundsException: Index: 3" or similar.

  2. Could the reason for missing stack-trace, be that this code is dynamically generated at runtime, and as such the logger/JVM does not "know" the stack-trace or relevant line number(s)?

We are currently debugging and investigating to get more info, but maybe this sounds familiar to somebody.

like image 454
Christoph Avatar asked Sep 25 '12 11:09

Christoph


People also ask

How do I get rid of ArrayIndexOutOfBoundsException?

Here are few handy tips to avoid ArrayIndexOutOfBoundsException in Java: Always remember that the array is a zero-based index, the first element is at the 0th index and the last element is at length - 1 index. Pay special attention to the start and end conditions of the loop. Beware of one-off errors like above.

What causes ArrayIndexOutOfBoundsException?

What Causes ArrayIndexOutOfBoundsException. The ArrayIndexOutOfBoundsException is one of the most common errors in Java. It occurs when a program attempts to access an invalid index in an array i.e. an index that is less than 0, or equal to or greater than the length of the array.

Is ArrayIndexOutOfBoundsException a checked exception?

ArrayIndexOutofBoundsException is an unchecked exception. Therefore, the java compiler will not check if a given block of code throws it.

What type of error is ArrayIndexOutOfBoundsException?

The ArrayIndexOutOfBoundsException is a Runtime Exception thrown only at runtime. The Java Compiler does not check for this error during the compilation of a program.


2 Answers

  1. String concatenated with a null reference might get you such a message:

    Object obj = null; throw new ArrayIndexOutOfBoundsException("" + obj); 
  2. If you're using an Oracle JVM you may want to add -XX:-OmitStackTraceInFastThrow as an additional parameter to see if it helps. For some basic exceptions, the JVM reuses the same exception instance after a while, in which case there's no stack trace anymore. This option prevents the reuse, so you always get a stack trace.

Edit note: this last could also apply to a recent version of OpenJDK (e.g., 1.8)

like image 140
Frank Pavageau Avatar answered Sep 28 '22 09:09

Frank Pavageau


I found pretty much the same behavior with disappointing log as shown below:

java.lang.ArrayIndexOutOfBoundsException: null 

In my case, the problem was in concurrent ArrayList.add calls (two separate threads added elements into shared unsynchronized list). The most interesting thing: first ArrayIndexOutOfBoundsException always had stacktrace and descriptive message, all future ArrayIndexOutOfBoundsExceptions were without stacktrace and message. Tried to reproduce on separate project with plain java threads - no luck (always got full stacktraces etc).

PS. OpenJDK 11, jetty server.

like image 39
Volodymyr Kret Avatar answered Sep 28 '22 10:09

Volodymyr Kret