Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6120245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:38:48+00:00 2026-05-23T15:38:48+00:00

Edit: Well, I feel rather sheepish. I was looking at the wrong constructor. The

  • 0

Edit: Well, I feel rather sheepish. I was looking at the wrong constructor. The real constructor being called (see below) -was- violating the concurrency rules of the foreach loop, as per Kal’s answer.

Thank you for your help regardless! It will still help me fix the actual error with the code.

all

I’m a pretty new Java Programmer, and am only just starting to get a basic handle on the language. I’m currently working with a dialogue participant system, but have first been trying to get our system’s representations of Logical Terms up to spec. I’m almost done, but have run into the following Error:

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at com.Term.<init>(Term.java:97)
at com.Term.substituteVariables(Term.java:251)
at com.Term.substituteVariables(Term.java:247)
at com.Term.substituteVariables(Term.java:247)
at com.TermPredTestArch.main(TermPredTestArch.java:40)

The method in question, substituteVariables, is basically a copy constructor, with a slight modification: it takes in a map of bindings, and recursively iterates through the Term Object that it is called from, finding variables along the way and swapping them out for their instantiations. Bizarrely, it seemed to be working just last night when I left (though I did not test extensively), but now refuses to play nice; I had made no substantial modifications.

The relevant code is as follows (lines 232-252):

232  /** Returns a new Term with the appropriate bindings substituted */
233  public Term substituteVariables(Map<Variable, Symbol> bindings) {
234    ArrayList<Symbol> args    = this.getArgs();
235    ArrayList<Symbol> newArgs = new ArrayList<Symbol>();
236    Set<Variable> vars        = this.getVars();
237    Set<Variable> bindingKeys = bindings.keySet();
238    for(Symbol s: args) {
239      // if s is a Variable, check to see if it has a substituion, and
240      // if so, swap it out
241      if(s instanceof Variable) {
242        if(bindingKeys.contains(s)) newArgs.add(bindings.get(s));
243        else                        newArgs.add(s);
244      // if s is a Term, add it and recursively substitute any variables
245      // it has within the current set of bindings
246      } else if(s instanceof Term) {
247        newArgs.add(((Term) s).substituteVariables(bindings));
248      // if s is just a symbol, simply add it to the args
249      } else newArgs.add(s);
250    }
251    return new Term(this.getName(), newArgs);
252  }

EDIT: And here is the constructor for Term:

public Term(String n, ArrayList<Symbol> a) {
    super(n);
    args = a;
    HashSet<Variable> varsToAdd = new HashSet<Variable>();
    for(Symbol s: a) parseArg(s.toString());
}          

That is the -actual- constructor that was being called, not the one that I thought was being called. Which does, in fact, violate the foreach loop concurrency rules, as per Kal’s answer.

From the research I’ve already done, I know that ConcurrentModificationException is often caused by multiple threads iterating/modifying a Collection simultaneously without synchronization, but I have no deliberate parallelism here, nor anywhere else in the class, or the test code that’s using it. Otherwise, I’m not entirely sure. the javadoc for the class mentions that it could also be cause by an iterator simultaneously iterating and modifying a Collection, but I don’t think I’m doing that either; I’m merely observing the iterated Collection and using information from it to build another Collection. Does that violate concurrency regulations as well?

Any pointers you all might be able to provide would be hugely appreciated! I will also pre-emptively apologise for any egregious breaches of Java etiquette or style (feel free to point those out too!).

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:38:48+00:00Added an answer on May 23, 2026 at 3:38 pm

    ConcurrentModificationException occurs when you modify an ArrayList while iterating through it using the for loop.

    The way to do this correctly is to use the Iterators add/remove methods.

    Here is the relevant documentation from the API —

    The iterators returned by this class’s iterator and listIterator methods are fail-fast: if list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: I have solved this by myself. See my answer below I have set
edit #2: Question solved halfways. Look below As a follow-up question, does anyone know
Feel free to edit the title if you know how to formulate the question
I hope the title is chosen well enough to ask this question. Feel free
I can't describe the problem well enough in the title. If somone has edit
I'm looking into HTML5 and I'm puzzled why it goes so easy on well-formedness.
Edit: This question was written in 2008, which was like 3 internet ages ago.
Edit: From another question I provided an answer that has links to a lot
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.