Skip to content

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

@scottfones

Description

@scottfones

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"));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions