IndexOutOfBounds with Index 14, size 16. How?

江枫思渺然 提交于 2020-01-14 18:46:14

问题


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)

回答1:


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

  • 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



回答2:


I have the same exception in sun.print.Win32MediaSize too, and was helped by the answer, it is a concurrency issue.

Log output:

java.lang.IndexOutOfBoundsException: Index: 14, Size: 15
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at sun.print.Win32MediaSize.findMediaName(Win32PrintService.java:1742)
at sun.print.Win32PrintService.initMedia(Win32PrintService.java:418)
at sun.print.Win32PrintService.getSupportedAttributeValues(Win32PrintService.java:1367)
at sun.print.RasterPrinterJob.updatePageAttributes(RasterPrinterJob.java:548)
at sun.print.RasterPrinterJob.setPrintable(RasterPrinterJob.java:997)
at net.sf.jasperreports.engine.export.JRPrintServiceExporter.exportReport(JRPrintServiceExporter.java:467)
  • for the constructor public Win32MediaSize(String name, int dmPaper) if thread 1 add a string to winStringTable, but do not add object to winEnumTable
  • then if thread 2 call the method findMediaName, it will caused the indexoutofbounds exception.

Code bellow

package sun.print;

import java.util.ArrayList;

import javax.print.attribute.EnumSyntax;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;

class Win32MediaSize extends MediaSizeName {
    private static ArrayList winStringTable = new ArrayList();
    private static ArrayList winEnumTable = new ArrayList();
    private static MediaSize[] predefMedia;
    private int dmPaperID; // driver ID for this paper.

    private Win32MediaSize(int x) {
        super(x);

    }

    private synchronized static int nextValue(String name) {
      winStringTable.add(name);
      return (winStringTable.size()-1);
    }

    public static synchronized Win32MediaSize findMediaName(String paramString) {
        int i = winStringTable.indexOf(paramString);
        if (i != -1) {
            return ((Win32MediaSize) winEnumTable.get(i));
        }
        return null;
    }

    public static MediaSize[] getPredefMedia() {
        return predefMedia;
    }

    public Win32MediaSize(String name, int dmPaper) {
        super(nextValue(name));
        dmPaperID = dmPaper;
        winEnumTable.add(this);
    }

    private MediaSizeName[] getSuperEnumTable() {
      return (MediaSizeName[])super.getEnumValueTable();
    }

    static {
        Win32MediaSize localWin32MediaSize = new Win32MediaSize(-1);

        MediaSizeName[] arrayOfMediaSizeName = localWin32MediaSize
                .getSuperEnumTable();
        if (arrayOfMediaSizeName != null) {
            predefMedia = new MediaSize[arrayOfMediaSizeName.length];

            for (int i = 0; i < arrayOfMediaSizeName.length; ++i)
                predefMedia[i] = MediaSize
                        .getMediaSizeForName(arrayOfMediaSizeName[i]);
        }
    }

    int getDMPaper() {
        return dmPaperID;
    }

    protected String[] getStringTable() {
      String[] nameTable = new String[winStringTable.size()];
      return (String[])winStringTable.toArray(nameTable);
    }

    protected EnumSyntax[] getEnumValueTable() {
      MediaSizeName[] enumTable = new MediaSizeName[winEnumTable.size()];
      return (MediaSizeName[])winEnumTable.toArray(enumTable);
    }

    }

Edit:

i'm doing a print system with 5 thread to call java print api, and got the same exception sometimes. i researched a lot until here i see the concurreny point.
i fixed my problem by init the printService by only 1 thread, i record it here to remind there is concurreny issue in class sun.print.Win32MediaSize as i describle above.



来源:https://stackoverflow.com/questions/13536340/indexoutofbounds-with-index-14-size-16-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!