Stacktrace points to line after end of file?

I get in my program an unexpected ConcurrentModificationException. The stacktrace for this exception, points to line 258 of the file - but the file has only 257 lines!

I have recompiled the file with same result.

The error happens inside a forEach{} loop, and inside that loop are some calls to inline functions that I didn’t use before: checkNotNull().

Could this somehow upset the line count?? The previous line in the stacktrace is exactly where it should be, calling the function named in the stacktrace with line 258, and below the function giving the error there are 3 other functions in the file.

The exception could be caused by calling Hibernate inside the loop, what I’m really puzzled by however is the odd line number.

Caused by: java.util.ConcurrentModificationException: null
	at java.util.ArrayList$Itr.checkForComodification( ~[na:1.8.0_131]
	at java.util.ArrayList$ ~[na:1.8.0_131]
	at org.hibernate.collection.internal.AbstractPersistentCollection$ ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
	at nl.l1nda.jobmp.marketplace.service.impl.DefaultShiftApplicantService.checkShiftApplicants(DefaultShiftApplicantService.kt:258) ~[classes/:na]
	at nl.l1nda.jobmp.marketplace.service.impl.DefaultShiftApplicantService.addShiftApplicant(DefaultShiftApplicantService.kt:200) ~[classes/:na]

This means that the actual source line is an inlined code fragment from somewhere else. As class files (in the proper debug information) only support specifying a single source file kotlin had to use a workaround. Basically it adds a table in the class file with mappings between line number ranges and source files. The line numbers used for this are outside the actual range of line numbers of the file. The debugger/ide will “fix” it up for you, but exceptions don’t do that.

OK, thanks. It probably happened in the forEach expansion.

Right, but how do we convert the exception’s stack trace to the actual one? Is there a tool for that?

You can analyze the stacktrace in IDEA. It could navigate either to call site or to the function body of an inline function from such a stacktrace.

I tried using Analyze → Analyze Stacktrace… for an inline function crash, but the trace still points to a line at the end of the file.
Is there something else I need to do, or is a specific version of IDEA needed to make this work? I’m using Android Studio 3.1

1 Like

By the way, is it possible for the Kotlin compiler to include values of temporary vars and method parameters in the stack trace? We had this in the good old times of Smalltalk and it was very useful when analyzing the stack trace.

System.getProperty call caused this for me

Try to normalize the stack trace in the Analyze Stack Trace dialog first. For me (tested with an inline function) it then works as it should (clicking on the link in the trace asks me whether I want to go to the function body or the call site).

1 Like

This actually worked for me!

Could you provide a library for line number resolving in runtime? The strange line number break many things… (Such as Inline all logging functions · Issue #34 · oshai/kotlin-logging · GitHub)