NPE when resizing SCollection due to null inherited bounds #31

Open
opened 2026-03-27 17:48:10 +00:00 by thib8956 · 0 comments
Owner

Problem:
Resizing an SCollection throws a NullPointerException: this.bounds is null.

Investigation:

  • SCollection extends AbstractShape but its constructor calls super() without initializing the inherited bounds, leaving it null (src/main/java/ovh/gasser/newshapes/shapes/SCollection.java:21).
  • AbstractShape.resize() assumes bounds is non-null and dereferences it (src/main/java/ovh/gasser/newshapes/shapes/AbstractShape.java:47).
  • During mouse drag resize, Controller.handleMouseDragged() calls shape.resize(...), triggering the NPE (src/main/java/ovh/gasser/newshapes/ui/Controller.java:96).
  • SCollection derives its bounds dynamically from children (overrides getBounds()) instead of storing the inherited bounds field like other shapes.

Recommended minimal fix:

  1. Prefer overriding resize() in SCollection so resizing operates on the collection by delegating/resizing children or recomputing bounds, avoiding reliance on the inherited bounds field.
  2. As a secondary safeguard, consider adding a null guard in AbstractShape.resize() to avoid dereferencing bounds when it is null (fail-safe or throw informative exception).

Files referenced:

  • src/main/java/ovh/gasser/newshapes/shapes/AbstractShape.java:47
  • src/main/java/ovh/gasser/newshapes/shapes/SCollection.java:21
  • src/main/java/ovh/gasser/newshapes/ui/Controller.java:96

Please implement the preferred approach: override resize() in SCollection to handle resizing correctly for collections, and add a small defensive null check in AbstractShape.resize() as a secondary safeguard if convenient.

Please:

  1. Edit issue #31 with the corrected body.
  2. Return confirmation and the final body used.
Problem: Resizing an SCollection throws a NullPointerException: this.bounds is null. Investigation: - SCollection extends AbstractShape but its constructor calls super() without initializing the inherited bounds, leaving it null (src/main/java/ovh/gasser/newshapes/shapes/SCollection.java:21). - AbstractShape.resize() assumes bounds is non-null and dereferences it (src/main/java/ovh/gasser/newshapes/shapes/AbstractShape.java:47). - During mouse drag resize, Controller.handleMouseDragged() calls shape.resize(...), triggering the NPE (src/main/java/ovh/gasser/newshapes/ui/Controller.java:96). - SCollection derives its bounds dynamically from children (overrides getBounds()) instead of storing the inherited bounds field like other shapes. Recommended minimal fix: 1. Prefer overriding resize() in SCollection so resizing operates on the collection by delegating/resizing children or recomputing bounds, avoiding reliance on the inherited bounds field. 2. As a secondary safeguard, consider adding a null guard in AbstractShape.resize() to avoid dereferencing bounds when it is null (fail-safe or throw informative exception). Files referenced: - src/main/java/ovh/gasser/newshapes/shapes/AbstractShape.java:47 - src/main/java/ovh/gasser/newshapes/shapes/SCollection.java:21 - src/main/java/ovh/gasser/newshapes/ui/Controller.java:96 Please implement the preferred approach: override resize() in SCollection to handle resizing correctly for collections, and add a small defensive null check in AbstractShape.resize() as a secondary safeguard if convenient. Please: 1. Edit issue #31 with the corrected body. 2. Return confirmation and the final body used.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: thib8956/new-shapes#31