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 7057341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:58:28+00:00 2026-05-28T03:58:28+00:00

I am new to Java and especially to iterating through lists and modifying it’s

  • 0

I am new to Java and especially to iterating through lists and modifying it’s elements. I went through dozens of similar questions, but after many tries nothing worked.
In the following code sample an exceptions is thrown. It is not related to concurrent thread as I only have one thread. Netbeans output sais that the exception occurs in line 5(CustomerData customer = i.next();) :

CustomerData tempCustomer = new CustomerData("",tagID,0);
tempCustomer.setName(name);
tempCustomer.setSize(size);
for(ListIterator<CustomerData> i = customers.listIterator(); i.hasNext(); )
{
    CustomerData customer = i.next();
    if(customer.getTagID().contains(tagID))
    {
        Object[] options = {"overwrite", "do not overwrite"};
        int n = JOptionPane.showOptionDialog(rootPane, 
                                            "TagID already exists. overwrite?", 
                                            "?", 
                                            JOptionPane.YES_NO_OPTION, 
                                            JOptionPane.QUESTION_MESSAGE, 
                                            null, 
                                            options, 
                                            rootPane);
       if ( n == JOptionPane.YES_OPTION ){
           i.set(tempCustomer); 
       }
    }
    else{
        addCustomer();
    }
}

The whole code is only supposed to check if the element has matching tagID and if it does, replace two values (name and size) in it. Initially I tried to use setName() and setSize() inside the for loop on the element, but after it didn’t work and after reading other related questions, I assigned the values to temporary object before the loop and the used the iterator’s set method to replace current element. But still no success and it even seems it never gets that far because the exception comes after line 5…

  • 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-28T03:58:29+00:00Added an answer on May 28, 2026 at 3:58 am

    The problem is presumably that this line:

            addCustomer();
    

    is trying to modify customers while you’re in the middle of iterating over it. This isn’t allowed.

    And anyway, it seems like a logic error: presumably you want to call addCustomer only once, after the loop, if no customer had the right tagID, but your current code tries to call addCustomer for each customer with with the wrong tagID. So you need to use a boolean variable during the loop to keep track of whether any customer matched, and then, after the loop, call addCustomer if appropriate.

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

Sidebar

Related Questions

Eclipse and MyEclipse create new Java files with an extra blank line after the
I am relatively new to the world of Java and Maven, but I couldn't
I am relatively new to programming and especially to Java... So it takes me
I'm really new to Java, especially working with GUI in Java. I want to
So, I'm both new to Java and to creating android apps, but not new
For a new Java web project I thought about using: Eclipse as IDE Mercurial
Almost every new Java-web-project is using a modern MVC-framework such as Struts or Spring
I'm a relatively new Java programmer coming from C++/STL, and am looking for a
We are starting a new Java EE project and am looking for suggestions regarding
When creating a new Java project in IntelliJ IDEA, the following directories and files

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.