-2
private void btnOKActionPerformed(java.awt.event.ActionEvent evt)                                      
    {                                          
        try
        {
            if(txtStaffID.getText(0, 2).equals("AD"))
            {
                for(Admin admin:admincontrolpanel.adminList)
                {
                    if(txtUsername.getText().equals("") || txtPassword.getText().equals("") || txtName.getText().equals("") || txtEmail.getText().equals("") || txtContactNumber.getText().equals("") || txtICNumber.getText().equals(""))
                    {
                        JOptionPane.showMessageDialog(null, "Please fill in the blank", "ERROR", JOptionPane.ERROR_MESSAGE);
                    }
                    else if(txtUsername.getText() != null && txtPassword.getText() != null && txtName.getText() != null && txtEmail.getText() != null && txtContactNumber.getText() != null && txtICNumber.getText() != null)
                    {
                        admin.setId(txtStaffID.getText());
                        admin.setUsername(txtUsername.getText());
                        admin.setPassword(txtPassword.getText());
                        admin.setName(txtName.getText());
                        admin.setEmail(txtEmail.getText());
                        admin.setContactNumber(txtContactNumber.getText());
                        admin.setIcNumber(txtICNumber.getText());
                        Admin newAdmin = new Admin(admin.getId(),admin.getUsername(),admin.getPassword(),admin.getName(),admin.getEmail(),admin.getContactNumber(),admin.getIcNumber());
                        admincontrolpanel.adminList.remove(admin);
                        admincontrolpanel.adminList.add(newAdmin);
                    }
                }
                JOptionPane.showMessageDialog(null, "Successfully Added!", "Add Staff", JOptionPane.PLAIN_MESSAGE);
                dispose();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

Inside the text file got these data...

AD001|jeff|jeff|jeff|jeff@gmail.com|123456|123456
AD002|admin|admin|admin|admin@gmail.com|123456|123456

When I press OK button to edi...it shows me this ConcurrentModificationException error....Anyone can help me to take a look of it?? Admin is the class...adminList is the arraylist...

        try
        {
            PrintWriter pw = new PrintWriter("Admin.txt");
            for(Admin admin:admincontrolpanel.adminList)
            {
                pw.println(admin.getId() + "|" + admin.getUsername() + "|" + admin.getPassword() + "|" + admin.getName() + "|" + admin.getEmail() + "|" + admin.getContactNumber() + "|" + admin.getIcNumber());
            }
            pw.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

This is the code i want to write into the text file from arraylist.... then it gave me this kind of data inside the arraylist...can edit it..but deleted the 1st data and add a new data into it

AD002|admin|admin|admin|admin@gmail.com|123456|123456
AD002|admin|admin|admin|admin@gmail.com|999|123456
jefferyleo
  • 630
  • 3
  • 17
  • 34

3 Answers3

0

A quick search through SO would have quickly shown you possible solutions..you can use a standard for loop and loop until the previous list's size, instead of using an implicit Iterator the way you are:

ArrayList<Admin> = admincontrolpanel.adminList;
int adminListSize = adminList.size(); 
for(int i=0;i<adminListSize;i++) {
   Admin admin = adminList.get(i);

Or you can use a ListIterator to loop through the list using an Iterator and modify it.

Also, admincontrolpanel is not a good variable name. Use camelCase.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
0

for(Admin admin:admincontrolpanel.adminList)

Through this loop you are iterating adminList(ArrayList)

    Admin newAdmin = new Admin(admin.getId(),admin.getUsername(),admin.getPassword(),admin.getName(),admin.getEmail(),
    admin.getContactNumber(),admin.getIcNumber());
    admincontrolpanel.adminList.remove(admin);
    admincontrolpanel.adminList.add(newAdmin);

above in last two line you are removing and adding ArrayList item which is not allowed here because you already iterating ArrayList. you can not do iterating and modifying ArrayList at the same time here. thats why you are facing Concurrent..exception.

java seeker
  • 1,246
  • 10
  • 13
0

This problem may occur due to thread inside another thread. For example if your are executing some code in your first thread and along this you have another thread inside this thread. then this problem occur.

Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81