'Cant figure out whats causing unsupportedOperationException Exception
its interesting because when i use the editcontact() method in main its perfectly fine but if i use it in Junit testing it throws the exception and i don't really know why.
public void editContact() throws IOException {
File file = new File("src/test/test.txt");
if (!file.exists())
throw new FileNotFoundException("Failed To find a file named test.txt in Test Read/Write Test Case");
PhoneContact temp = new PhoneContact("Giorgi", "Gagnidze", "9999", "[email protected]");
main.editContact("111", temp, file);
List<PhoneContact> retrieved = dao.getData(file);
assert (retrieved.contains(temp));
for (var objects : retrieved)
assert (objects.equals(testObjects.get(0)));
}
when i run this test i get the exception and the line causing it is
main.editContact("111", temp, file);
heres the editcontact method in main
public void editContact(String mobile, PhoneContact newContact, File file) {
Data data = new Data();
List<PhoneContact> phoneContact = data.getData(file);
int index = 0;
PhoneContact toEdit = StaticMainMethods.viewContact(mobile,file);
for (PhoneContact i1 : phoneContact){
if (i1.getPhoneNumber().equalsIgnoreCase(newContact.getPhoneNumber()) && !newContact.getPhoneNumber().equalsIgnoreCase(mobile) || i1.getEmail().equalsIgnoreCase(newContact.getEmail()) && !i1.getEmail().equalsIgnoreCase(toEdit.getEmail())){
System.out.println("Contact with similar number or email already exists");
return;
}
}
for (PhoneContact i : phoneContact) {
if (i.getPhoneNumber().equalsIgnoreCase(mobile)) {
phoneContact.set(index, newContact);
System.out.println("Edited contact");
data.saveData(phoneContact, file);
return;
}
index++;
}
System.out.println("Couldn't find the contact");
heres getData method
public List<PhoneContact> getData(File file) {
List<PhoneContact> phoneContact = new ArrayList<>();
try {
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
phoneContact = (List<PhoneContact>) inputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return phoneContact;
}
i've already read that asList() could cause the issue but that method is never used anywhere.
Solution 1:[1]
You get an UnsupportedOperationException most probably because you are operating on a read only list.
There are several ways to create a read only list, among others:
- Arrays.asList
- Collections.unmodifiableList
- List.of
- List.copyOf
Unfortunately, given a List<T>, you have no way to know whether it's read only unless you effectively try to add, remove or modify an element.
The standard library of Java doesn't provide any idiomatic and reliable way, and it's actually very sad.
IF you can do it, the best way to generally handle all situations is to always use a known List implementation in your class, such as ArrayList or LinkedList. IN setters, copy elements into your list rather than blindly replace a List reference coming from outside, and in getters, always return a preferably unmodifiable copy of the list when requested from outside. Short example:
public class MyObject {
private List<String> items = new ArrayList<>();
public void setItems (List<String> newItems) {
// Bad: items = newItems;
items.clear();
items.addAll(newItems);
}
public List<String> getItems () {
return Collections.unmodifiableList(items);
// good alternative: return List.copyOf(items);
// bad: return items
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | QuentinC |
