-3
class A{

    private List<String> users;

    public List<String> getusers(){
        users.trimToSize();
        return users;
    }


    class B{

        public static void main(String[] args){

            for(String user:getusers()){
                System.out.println("user is :"+user);
            }
        }

Above for loop is throwing ConcurrentModificationException Exception. How can I resolve it?

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
Anju Singh
  • 367
  • 1
  • 13
  • 1
    Did you do any prior research... Like searching for the exception message? – GhostCat Dec 05 '16 at 07:11
  • 4
    When I run it, I get a NullPointerException. What aren't you showing us? – Dawood ibn Kareem Dec 05 '16 at 07:12
  • i did not write exact code(Imade sample code) I cannot write my actual code here, my main question here is how can i avoid concurrentmodification exception here, – Anju Singh Dec 05 '16 at 07:14
  • just assume here that i have populated users list before iteration – Anju Singh Dec 05 '16 at 07:16
  • 1
    Well, you get a `ConcurrentModificationException` if you modify a collection while you're iterating through it. But you haven't shown any code that does that. If your code is totally different from the above, then there wasn't much point in showing the code that you showed. – Dawood ibn Kareem Dec 05 '16 at 07:19
  • I think you have are adding something new in arraylist. So collection is changed and because its fail-fast it throws such error ! – Darshit Dec 05 '16 at 07:21
  • 2
    Even if you had populated the list that the (instance) variable `users` refers to, the main method in class `B` does not have any reference to an instance of type `A`. Obviously you tried to make an [MCVE](http://stackoverflow.com/help/mcve), but forgot about the C (complete) and the V (verifiable). So, please change your code to being compilable and showing us the problem. – Seelenvirtuose Dec 05 '16 at 07:21
  • 1
    Your provided program is completely incomplete! – ScanQR Dec 05 '16 at 07:22

1 Answers1

0

This code will only throw a CME if two threads call getUsers concurrently.

To avoid the exception you can return a copy of the users list:

return new ArrayList<>(users);

or keep the list trimed at all times or ensure the list access is synchronized.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • I don't see any exception throwing in the source code of the `trimToSize` method in class `ArrayList`. So running the method `getusers()` concurrently does _not_ throw a CME. Apart from that I think OP is not using threads. The far more common cause of a CME is changing the collection while iterating over it. – Seelenvirtuose Dec 05 '16 at 07:28
  • This really isn't right. You can get a `ConcurrentModificationException` with just one thread, if you modify a collection _inside_ a loop that iterates through it. The most likely scenario here is that OP has some code inside her `for` loop that changes her `List` in some way. But since she refuses to show it, we'll never know for sure. – Dawood ibn Kareem Dec 05 '16 at 08:04
  • IMHO The OP's code as it stands can only throw a CME if another thread tries to work on the list while `trimToSize` is happening. – OldCurmudgeon Dec 05 '16 at 08:58
  • OK, good point. I've removed my downvote. With the code as it stands, I think you're right; although OP has indicated that the actual code in the question is a complete red herring. – Dawood ibn Kareem Dec 05 '16 at 09:27