JAVA java.util.ConcurrentModificationException:null 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28534402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:46:16  来源:igfitidea点击:

JAVA java.util.ConcurrentModificationException:null Exception

javaexceptionconcurrentmodification

提问by Pratik

I am working on my code and getting "java.util.ConcurrentModificationException". I googled it and also know that such error comes only when you try to modify on-going iterative variable, which I don't feel in my case. Below is my code.

我正在处理我的代码并收到“java.util.ConcurrentModificationException”。我用谷歌搜索了一下,也知道只有当你尝试修改正在进行的迭代变量时才会出现这样的错误,在我的情况下我不觉得。下面是我的代码。

// This line is having an error
for(Statement s: _mConsumableStatements)
    {
        System.out.println("s:"+s);

        Boolean isConflict=false;
        Resource uri=s.getSubject();

        System.out.println("uri:"+uri);
        System.out.println("1");

        if(RepStatementsAsHash.containsKey(uri))
        {
            System.out.println("2");

            if(RepStatementsAsHash.get(uri).containsKey(RDFS.LABEL))
                RepStatementsAsHash.get(uri).remove(RDFS.LABEL);
            else if(RepStatementsAsHash.get(uri).containsKey(RDFS.SUBCLASSOF))
                RepStatementsAsHash.get(uri).remove(RDFS.SUBCLASSOF);
            else if(RepStatementsAsHash.get(uri).containsKey(RDFS.RANGE))
                RepStatementsAsHash.get(uri).remove(RDFS.RANGE);
            else if(RepStatementsAsHash.get(uri).containsKey(RDF.TYPE))
            {
                System.out.println("2.1");

                Value RepTypeObject=RepStatementsAsHash.get(uri).get(RDF.TYPE);
                Value ConsumableTypeObject=CStatements.get(uri).get(RDF.TYPE);

                if (RepTypeObject.stringValue().equals(OWL.CLASS) || RepTypeObject.stringValue().equals(RDFS.CLASS))
                {
                    if(ConsumableTypeObject.stringValue().equals(OWL.DATATYPEPROPERTY) || ConsumableTypeObject.stringValue().equals(OWL.OBJECTPROPERTY) || ConsumableTypeObject.stringValue().equals(RDF.PROPERTY))
                    {
                        isConflict=true;
                        //_mConflictingStatements.add(s);
                    }
                    else
                    {
                        RepStatementsAsHash.get(uri).remove(RepTypeObject);
                    }
                }
                else if(RepTypeObject.stringValue().equals(OWL.DATATYPEPROPERTY) || ConsumableTypeObject.stringValue().equals(OWL.OBJECTPROPERTY) || RepTypeObject.stringValue().equals(RDF.PROPERTY))
                {
                    if (ConsumableTypeObject.stringValue().equals(OWL.CLASS) || ConsumableTypeObject.stringValue().equals(RDFS.CLASS))
                    {
                        isConflict=true;
                        //_mConflictingStatements.add(s);
                    }
                    else
                    {

                    }
                }
                else
                {
                    if( !(RepTypeObject.stringValue().equals(OWL.DATATYPEPROPERTY) || ConsumableTypeObject.stringValue().equals(OWL.OBJECTPROPERTY) || RepTypeObject.stringValue().equals(RDF.PROPERTY) || ConsumableTypeObject.stringValue().equals(OWL.CLASS) || ConsumableTypeObject.stringValue().equals(RDFS.CLASS))  )
                    {
                        if(RepTypeObject.stringValue()!=ConsumableTypeObject.stringValue())
                        {
                            isConflict=true;
                        }

                    }
                }
                System.out.println("2.2");
                if(!isConflict)
                    {
                        addStatementsInRepository(uri.stringValue(),c_import);
                    }
            } // LABEL-TYPE 
        }
        else // No  URI Conflict
            addStatementsInRepository(uri.stringValue(),c_import);

        System.out.println("3");
    }

It is showing error something like :

它显示的错误类似于:

11:13:24.405 ERROR - error while importing Sesame data:
java.util.ConcurrentModificationException: null
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859) ~[na:1.7.0_71]
    at java.util.ArrayList$Itr.next(ArrayList.java:831) ~[na:1.7.0_71]
    at org.apache.marmotta.platform.core.services.importer.rdf.RDFImporterImpl.importData2(RDFImporterImpl.java:433) [ulysses-core-1.0-alpha-2.jar:1.0-alpha-2]

How to resolve it?

如何解决?

回答by loafy

On this line, like many others in your code: RepStatementsAsHash.get(uri).remove(RepTypeObject);, you are iterating through the loop, and removing it whilst iterating. You can use an Iteratorto iterate through a list or map, and then remove elements using iterator.remove().

在这一行上,与您的代码中的许多其他人一样:RepStatementsAsHash.get(uri).remove(RepTypeObject);,您正在遍历循环,并在迭代时将其删除。您可以使用Iterator遍历列表或地图,然后使用iterator.remove().

Example

例子