Getting "ArrayIndexOutOfBoundsException: null" with NO stack trace
Asked Answered
P

2

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.

Polard answered 25/9, 2012 at 11:50 Comment(2)
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.Polard
Links to similar questions to help a stranger like me in the future :) NullPointerException in Java with no StackTrace and NullPointerException stack trace not available without debug agent.Lowther
G
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)

Goya answered 25/9, 2012 at 12:2 Comment(5)
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.Polard
I have got the same problem with an array. The parameter OmitStackTraceInFastThrow helps. Thanks.Hatchel
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.Lowther
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.Tunicle
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?Manuel
L
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.

Latty answered 6/6, 2019 at 9:39 Comment(2)
I can confirm this on OpenJDK 8, only first ArrayIndexOutOfBoundsException had stacktrace .Trachoma
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 helpEucharis

© 2022 - 2024 — McMap. All rights reserved.