Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formal and Partial Context: remove(I id) throws NullPointerException for non-existent objects #3

Open
scottfones opened this issue Nov 9, 2021 · 0 comments

Comments

@scottfones
Copy link

FormalContext and PartialContext both throw a NullPointerException when attempting to remove an object not present in the context by the object's id.

The code is equivalent:

@Override
public boolean removeObject(I id) throws IllegalObjectException {
	boolean removed = getObjects().remove(getObject(id));
	if (!removed) {
		throw new IllegalObjectException("Object" + id + "not successfully removed");
	}
	return true;
}

On the first line, getObject(id) returns null for the non-existent object. Since the objects are held in a ListSet, getObjects().remove(...) calls ListSet's overridden remove(Object o) method.

ListSet#remove():

@Override
public boolean remove(Object o) throws NullPointerException {
	if (o == null) {
		throw new NullPointerException();
	}
	if (contains(o)) {
		this.elements.remove(o);
		return true;
	}
	return false;
}

As o is null, the NullPointerException is thrown instead of the IllegalObjectException that is expected.

Test to reproduce:

@Test
@Disabled("Throws NullPointerException")
void testRemoveObject() throws IllegalObjectException {
    PartialObject<Integer, String> pObjA = new PartialObject<>("a");
    PartialObject<Integer, String> pObjB = new PartialObject<>("b");

    PartialContext<Integer, String, PartialObject<Integer, String>> context = new PartialContext<>();

    context.addObject(pObjA);
    assertThrows(IllegalObjectException.class, () -> context.removeObject("b"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant