Skip to content

Commit fdf35cb

Browse files
committed
Merge pull request #540 from JLLeitschuh/fix/nullPointerInExceptionWitness
Fixes Null Pointer from ExceptionWitnessResponderButton
2 parents 88b8114 + 3426a01 commit fdf35cb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

ui/src/main/java/edu/wpi/grip/ui/components/ExceptionWitnessResponderButton.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ private synchronized ExceptionPopOver getPopover() {
160160
final ExceptionPopOver newPopOver = new ExceptionPopOver(this.popOverTitle);
161161
// Scene and root are null when the the constructor is called
162162
// They are not null once they are added to a scene.
163+
assert getScene() != null : "Scene should not be null, this object has not been added to the scene yet";
164+
assert getScene().getRoot() != null : "Root should not be null, this object has not been added to the scene yet";
163165
final Parent root = getScene().getRoot();
164166
newPopOver.getRoot().getStylesheets().addAll(root.getStylesheets());
165167
newPopOver.getRoot().setStyle(root.getStyle());
@@ -170,6 +172,10 @@ private synchronized ExceptionPopOver getPopover() {
170172

171173
@Subscribe
172174
public void onExceptionEvent(ExceptionEvent event) {
175+
if (getScene() == null || getScene().getRoot() == null) {
176+
// We are responding to an event prior to this button being added to the scene. Ignore this for now.
177+
return;
178+
}
173179
if (event.getOrigin().equals(origin)) {
174180
// Not timing sensitive. Can remain Platform.runLater
175181
Platform.runLater(() -> {
@@ -184,6 +190,10 @@ public void onExceptionEvent(ExceptionEvent event) {
184190

185191
@Subscribe
186192
public void onExceptionClearedEvent(ExceptionClearedEvent event) {
193+
if (getScene() == null || getScene().getRoot() == null) {
194+
// We are responding to an event prior to this button being added to the scene. Ignore this for now.
195+
return;
196+
}
187197
if (event.getOrigin().equals(origin)) {
188198
// Not timing sensitive. Can remain Platform.runLater
189199
Platform.runLater(() -> {

ui/src/test/java/edu/wpi/grip/ui/components/ExceptionWitnessResponderButtonTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,19 @@ private void flagNewException() {
7979

8080
private void flagWarning() {
8181
witness.flagWarning("A warning without a stacktrace");
82+
}
83+
84+
@Test
85+
public void testNoNullPointerBeforeAddedToScene() {
86+
final EventBus eventBus = new EventBus();
87+
final Object witnessed = new Object();
88+
final ExceptionWitness witness = new MockExceptionWitness(eventBus, witnessed);
8289

90+
final ExceptionWitnessResponderButton button = new ExceptionWitnessResponderButton(witnessed, "Test Button Popover");
91+
eventBus.register(button);
92+
witness.flagWarning("Warning message");
93+
witness.clearException();
94+
// We should not get a null pointer because of any of this
8395
}
8496

8597
}

0 commit comments

Comments
 (0)