31

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.

Chris W.
  • 1,680
  • 16
  • 35
Christoph
  • 3,980
  • 2
  • 40
  • 41
  • Edited title to make it more obvious that this is not a duplicate. This question here is more about the reason why there is no stack trace, and not how to avoid the exception. – Christoph Feb 15 '16 at 14:12
  • 1
    Links to similar questions to help a stranger like me in the future :) [NullPointerException in Java with no StackTrace](https://stackoverflow.com/q/2411487) and [NullPointerException stack trace not available without debug agent](https://stackoverflow.com/q/1076191). – Yoory N. Feb 08 '18 at 11:45

2 Answers2

37
  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)

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Frank Pavageau
  • 11,477
  • 1
  • 43
  • 53
  • 2
    Thanks for the -XX:-OmitStackTraceInFastThrow. Didn't know that flag. Seems to be the reason why the exception didn't appear anymore. We restarted the server (without setting this flag) and now we are seeing the stack trace again. – Christoph Sep 25 '12 at 13:01
  • I have got the same problem with an array. The parameter OmitStackTraceInFastThrow helps. Thanks. – gontard Jun 19 '14 at 08:51
  • 1
    When using some kind of logging framework (logback in this case), exception with no message (e.g. `throw new MyCoolException();`) will result in the output similar to `java.lang.ArrayIndexOutOfBoundsException: null` as the logger will concatenate exception's message (null in this case) to other part of the log line behind the scenes. – Yoory N. Feb 08 '18 at 11:40
  • I am running an OpenJDK JVM, version 1.8.0u171 (Debian 9), and it seems to accept the `-XX:-OmitStackTraceInFastThrow` flag as well. I've yet to confirm if that was why I was also failing to print stack-traces (e.g., using `e.printStackTrace`), but it seems highly likely. I've edited the answer to reflect this discovery. – Chris W. Jun 05 '18 at 17:09
  • so, per the 1st possibility, it's not the actual array access with an invalid index that causes AIOBE with a null message? it's just the manual exception throw with a custom message? – asgs Sep 01 '22 at 11:04
5

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.

Volodymyr Kret
  • 1,511
  • 15
  • 16
  • 3
    I can confirm this on OpenJDK 8, only **first** ArrayIndexOutOfBoundsException had stacktrace . – 8.8.8.8 Jun 26 '19 at 07:42
  • at first time java.util.ConcurrentModificationException: null at java.util.ArrayList$Itr.next(ArrayList.java:865) ~[?:1.8.0-zing_19.12.101.0] i got this , i tried to clean the array by calling clear but didn't help – JustTry Feb 25 '21 at 23:00