question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

java.lang.IndexOutOfBoundsException depending on the location of base class

See original GitHub issue

Describe the bug So this is a weird one. I just got a java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 compile error when trying to serialize a subclass of an abstract class in a UnitTest. (See the stack trace below)

The abstract base class looks like this:

@Serializable
abstract class ContentObject(val uid: String = UUID.randomUUID().toString()) 

The UnitTest is in another file and looks like this:

    @Test
    fun `Child class of ContentObject has uid`(){
        @Serializable
        class TestClass : ContentObject()

        val map = Mapper.map(TestClass.serializer(),TestClass())
        assertEquals(false,map.isEmpty())
    }

Trying to find out what the problem is, I copied the abstract base class into my UnitTest class. Having the base class inside of the UnitTest class fixes the issue, I am able to compile the UnitTest successfully. When removing the abstract base class from the UnitTest class again, the compilation fails and the same compile error occures.

This also happens when compiling for the android device directly (not using UnitTests).

To Reproduce Open Android Studio, create a new project. Put abstract base class (with property as shown above) in it’s own file in the “main” app scope. Put UnitTest in the “test” app scope. Compile. (Alternatively, look at this sample project: KotlinCompileError-IndexOutOfBoundsException.zip)

Expected behavior The IndexOutOfBoundsException compile exception shouldn’t occur for a class that when moved to another place compiles successfully. I guess there seems to be something weird going on with the code generation?

Environment

  • Kotlin version: 1.3.41 / 1.3.50-eap-54
  • Library version: 0.11.1
  • Kotlin platforms: JVM (Android)
  • Gradle version: 5.1.1
  • IDE version: Android Studio 3.4.2

Stack Trace

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

	at java.util.ArrayList.rangeCheck(ArrayList.java:653)
	at java.util.ArrayList.get(ArrayList.java:429)
	at kotlinx.serialization.internal.SerialClassDescImpl.getElementName(SerialClassDescImpl.kt:80)
	at kotlinx.serialization.NamedValueEncoder.elementName(Tagged.kt:159)
	at kotlinx.serialization.NamedValueEncoder.getTag(Tagged.kt:156)
	at kotlinx.serialization.NamedValueEncoder.getTag(Tagged.kt:155)
	at kotlinx.serialization.TaggedEncoder.encodeStringElement(Tagged.kt:122)
	at com.dev.haasedev.wooner.contentObjects.ContentObject.write$Self(ContentObject.kt:18)
	at com.dev.haasedev.wooner.DatabaseSerializerTest$Child class of ContentObject has uid$TestClass.write$Self(DatabaseSerializerTest.kt)
	at com.dev.haasedev.wooner.DatabaseSerializerTest$Child class of ContentObject has uid$TestClass$$serializer.serialize(DatabaseSerializerTest.kt)
	at com.dev.haasedev.wooner.DatabaseSerializerTest$Child class of ContentObject has uid$TestClass$$serializer.serialize(DatabaseSerializerTest.kt:141)
	at kotlinx.serialization.Encoder$DefaultImpls.encodeSerializableValue(Coders.kt:42)
	at kotlinx.serialization.TaggedEncoder.encodeSerializableValue(Tagged.kt:21)
	at kotlinx.serialization.CoreKt.encode(Core.kt:73)
	at kotlinx.serialization.Mapper.map(Mapper.kt:101)
	at kotlinx.serialization.Mapper$Companion.map(Mapper.kt:138)
	at com.dev.haasedev.wooner.DatabaseSerializerTest.Child class of ContentObject has uid(DatabaseSerializerTest.kt:157)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
tpgillamcommented, Aug 23, 2019

I have just ran into this issue (or something closely related) in Kotlin 1.3.50, however did not have any issue with 1.3.41. I’ve put the stack trace for my case below.

I came across this thread before coming up with a minimal failing example, however I do also have an abstract class, and a serializable derived class. In my case both these classes are in the same file, but are used from elsewhere (including tests).

e: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:372)
	at java.base/java.util.ArrayList.get(ArrayList.java:458)
	at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.checkTypeArguments(SerializationPluginDeclarationChecker.kt:167)
	at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.checkType(SerializationPluginDeclarationChecker.kt:188)
	at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.checkTypeArguments(SerializationPluginDeclarationChecker.kt:167)
	at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.checkType(SerializationPluginDeclarationChecker.kt:188)
	at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.analyzePropertiesSerializers(SerializationPluginDeclarationChecker.kt:155)
	at org.jetbrains.kotlinx.serialization.compiler.diagnostic.SerializationPluginDeclarationChecker.check(SerializationPluginDeclarationChecker.kt:44)
	at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.runDeclarationCheckers(ModifiersChecker.java:272)
	at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.checkModifierListCommon(ModifiersChecker.java:213)
	at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.checkModifiersForDeclaration(ModifiersChecker.java:172)
	at org.jetbrains.kotlin.resolve.DeclarationsChecker.process(DeclarationsChecker.kt:91)
	at org.jetbrains.kotlin.resolve.BodyResolver.resolveBodies(BodyResolver.java:245)
	at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations(LazyTopDownAnalyzer.kt:225)
	at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations$default(LazyTopDownAnalyzer.kt:60)
	at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:111)
	at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:81)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:555)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:82)
	at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:107)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:546)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:177)
	at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:164)
	at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:54)
	at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:84)
	at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:42)
	at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:104)
	at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:349)
	at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:105)
	at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally(IncrementalCompilerRunner.kt:237)
	at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.access$compileIncrementally(IncrementalCompilerRunner.kt:37)
	at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner$compile$2.invoke(IncrementalCompilerRunner.kt:79)
	at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:91)
	at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:606)
	at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:99)
	at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1645)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:359)
	at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
	at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
	at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
	at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
	at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
1reaction
SteynSieveocommented, Sep 7, 2019

I can confirm what @Brainfree said is what is happening to me to. When I remove the typealias I don’t get the exception during the build anymore.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why am I getting this IndexOutOfBoundsException error?
The reason is that you are passsing a value that exceeds the size of the list so clearly you are passing a parameter...
Read more >
Java IndexOutOfBoundsException “Source Does Not Fit in Dest”
In Java, making a copy of a List can sometimes produce an IndexOutOfBoundsException: “Source does not fit in dest”. In this short tutorial, ......
Read more >
Build fails with java.lang.IndexOutOfBoundsException: Index 6 ...
Build fails with java.lang.IndexOutOfBoundsException: Index 6 out of bounds for length 6 when referencing class in xmlutil-android.
Read more >
MWNumericArray - MathWorks
This method returns a one-dimensional array of imaginary elements of the Java primitive type byte . java.lang.Object, getImagData(). Returns a 1-D array ...
Read more >
How to Swap Two Elements in an ArrayList in Java?
Exception: It throws IndexOutOfBoundsException if the index of Array List is less than 0 or greater than the size of the ArrayList. Example...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found