IndexOutOfBounds 与索引 14,大小 16.如何? [英] IndexOutOfBounds with Index 14, size 16. How?

查看:40
本文介绍了IndexOutOfBounds 与索引 14,大小 16.如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当索引实际上在堆栈跟踪显示的范围内时,它怎么会超出范围?尽管上下文可能无关紧要,但我们正在为 Netbeans 平台上的 IDE 开发 Lua 解析器/VM,并且这种情况一直在增加.怎么会这样?一些奇怪的并发问题?提前感谢您提供任何见解.

How can the index be out of bounds when it actually is in bounds as shown by the stacktrace? Although the context may not matter we are working on a Lua parser/VM for an IDE on the Netbeans platform and this keeps creeping up. How can this be? Some strange concurrency issue? Thanks in advance for any insights.

java.lang.IndexOutOfBoundsException: Index: 14, Size: 16
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at org.netbeans.lib.lexer.BatchTokenList.existingToken(BatchTokenList.java:197)
    at org.netbeans.lib.lexer.BatchTokenList.tokenOffset(BatchTokenList.java:150)
    at org.netbeans.api.lexer.TokenSequence.offset(TokenSequence.java:256)
    at com.MYDevelopers.LuaSupportCompiler.TokenManager.getTokenStart(TokenManager.java:230)
    at com.MYDevelopers.LuaSupportCompiler.CompilationUnit.getCurrentLocation(CompilationUnit.java:459)
    at com.MYDevelopers.LuaSupportCompiler.CompilationUnit.expressionImp(CompilationUnit.java:654)
    at com.MYDevelopers.LuaSupportCompiler.CompilationUnit.expression(CompilationUnit.java:647)
    at com.MYDevelopers.LuaSupportCompiler.CompilationUnit.RHSexpression(CompilationUnit.java:643)
    at com.MYDevelopers.LuaSupportCompiler.CompilationUnit.chunk(CompilationUnit.java:1004)
    at com.MYDevelopers.LuaSupportCompiler.CompilationUnit.compile(CompilationUnit.java:164)
    at com.MYDevelopers.LuaSupportCompiler.CompilationUnit.compileIfRequired(CompilationUnit.java:148)
    at com.MYDevelopers.LuaSupport.LuaProject.CompilationManagers.SourcesManager.compile(SourcesManager.java:222)
    at com.MYDevelopers.LuaSupport.LuaProject.CompilationManagers.SourcesManager.compileAndEvaluateIfRequired(SourcesManager.java:210)
    at com.MYDevelopers.LuaSupport.LuaProject.CompilationManagers.SourcesManager.addSourceManager(SourcesManager.java:113)
    at com.MYDevelopers.LuaSupport.LuaProject.CompilationManagers.SourcesManager.addDirectory(SourcesManager.java:106)
    at com.MYDevelopers.LuaSupport.LuaProject.CompilationManagers.SourcesManager.addBootDirectory(SourcesManager.java:80)
    at com.MYDevelopers.LuaSupport.LuaProject.CompilationManagers.SourcesManager.addBaseLibraries(SourcesManager.java:72)
    at com.MYDevelopers.LuaSupport.LuaProject.CompilationManagers.SourcesManager.<init>(SourcesManager.java:47)
    at com.MYDevelopers.LuaSupport.LuaProject.CompilationManagers.GlobalCompilationManager.addProjectDirectory(GlobalCompilationManager.java:76)
    at com.MYDevelopers.LuaSupport.LuaProject.LuaProject.getSourcesManager(LuaProject.java:309)
    at com.MYDevelopers.LuaSupport.LuaProject.LuaProject$ProjectOpenHookImpl.projectOpened(LuaProject.java:599)
    at org.netbeans.spi.project.ui.ProjectOpenedHook$1.projectOpened(ProjectOpenedHook.java:84)
[catch] at org.netbeans.modules.project.ui.OpenProjectList.notifyOpened(OpenProjectList.java:1138)
    at org.netbeans.modules.project.ui.OpenProjectList.access$1600(OpenProjectList.java:134)
    at org.netbeans.modules.project.ui.OpenProjectList$LoadOpenProjects.loadOnBackground(OpenProjectList.java:434)
    at org.netbeans.modules.project.ui.OpenProjectList$LoadOpenProjects.run(OpenProjectList.java:312)
    at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:1452)
    at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:2032)

推荐答案

通过阅读ArrayList的源码,可以看到rangeCheck是实现为

By reading the source of ArrayList, you can see that rangeCheck is implemented as

 private void rangeCheck(int index) {
   if (index >= size)
     throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 }

所以我的猜测是并发.没有其他明显的方法可以在抛出此异常时使用较小的索引.

so my guesses just go to concurrency. There is not other obvious way to have a smaller index while having this exception thrown.

在抛出异常之后,大小发生了某种变化,在这种情况下

Somehow size changes after that the exception is thrown, in a situation in which

  • 您尝试从列表中检索超出范围的项目
  • 同时另一个线程正在添加另一个元素,因此 size 在抛出异常时增加了大小
  • 异常最终呈现
  • you try to retrieve an item from the list which is over bounds
  • at the same time another thread is adding another element so size increases over the size at the time the exception is thrown
  • the exception is finally rendered

这篇关于IndexOutOfBounds 与索引 14,大小 16.如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆