Ticket #1230 (closed enhancement: fixed)

Opened 11 months ago

Last modified 3 months ago

AbstractSelect.select(null) causes NPE in HashMap

Reported by: Jonatan Kronqvist Owned by: Artur Signell
Priority: minor Milestone: User Interface Library 5.3.0 RC
Component: Server-side framework Version:
Keywords: Cc:
Known Issue description:
Hours estimate: Deadline (dd.mm.yyyy):
Known Issue version (since): Known Issue title:
Hours done: Depends to:
Affects documentation: no
Known Issue workaround:
Affects release notes: yes Contract:

Description

As a convenience for developers I suggest allowing use of AbstractSelect.select(null);. This allows developers using the toolkit to skip manually converting all null itemIDs into Select.getNullSelectionItemId().

The current implementation is as follows:

public void select(Object itemId) {
	if (!isSelected(itemId) && items.containsId(itemId)) {
		if (isMultiSelect()) {
			final Set s = new HashSet((Set) getValue());
			s.add(itemId);
			setValue(s);
		} else if (itemId.equals(getNullSelectionItemId())) {
			setValue(null);
		} else {
			setValue(itemId);
		}
	}
}

My proposed fix:

public void select(Object itemId) {
	if (itemId == null)
		itemId = getNullSelectionItemId();
		
	if (!isSelected(itemId) && items.containsId(itemId)) {
		if (isMultiSelect()) {
			final Set s = new HashSet((Set) getValue());
			s.add(itemId);
			setValue(s);
		} else if (itemId.equals(getNullSelectionItemId())) {
			setValue(null);
		} else {
			setValue(itemId);
		}
	}
}

The fix is quite simple and should help developers avoid the trap of trying to select null and getting unexpected behavior.

Change History

Changed 6 months ago by Joonas Lehtinen

  • priority changed from undefined to minor
  • milestone set to User Interface Library 5.2.1

Changed 6 months ago by Joonas Lehtinen

  • milestone changed from User Interface Library 5.2.2 to User Interface Library 5.3.0 RC1

Changed 3 months ago by Joonas Lehtinen

  • owner changed from Jani Laakso to Artur Signell
  • status changed from new to assigned

Changed 3 months ago by Artur Signell

  • status changed from assigned to closed
  • resolution set to fixed

Fixed NPE and added possibility to use select(null) when there is a NullSelectionItem? [5249]

Note: See TracTickets for help on using tickets.