From 4cf94b5279ad87543fa43036c3368c93b8f40305 Mon Sep 17 00:00:00 2001 From: Matthias Arzt Date: Thu, 29 Aug 2024 11:06:44 +0200 Subject: [PATCH] Fix NullPointerException triggered by calling imgLabeling.getType().clear() The bug was discovered by compiling and testing Labkit against imglib2-7.1. Trying to use the brush in Labkit would trigger this NullPointerException. --- .../java/net/imglib2/roi/labeling/ImgLabeling.java | 2 +- .../net/imglib2/roi/labeling/ImgLabelingTest.java | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/imglib2/roi/labeling/ImgLabeling.java b/src/main/java/net/imglib2/roi/labeling/ImgLabeling.java index 2184d407..7ae615f7 100644 --- a/src/main/java/net/imglib2/roi/labeling/ImgLabeling.java +++ b/src/main/java/net/imglib2/roi/labeling/ImgLabeling.java @@ -243,7 +243,7 @@ public LabelingType< T > firstElement() @Override public LabelingType< T > getType() { - return new LabelingType<>( null, mapping, generation ); + return new LabelingType<>( indexAccessible.getType(), mapping, generation ); } @Override diff --git a/src/test/java/net/imglib2/roi/labeling/ImgLabelingTest.java b/src/test/java/net/imglib2/roi/labeling/ImgLabelingTest.java index d0ad40a6..7f1dc203 100644 --- a/src/test/java/net/imglib2/roi/labeling/ImgLabelingTest.java +++ b/src/test/java/net/imglib2/roi/labeling/ImgLabelingTest.java @@ -34,6 +34,7 @@ package net.imglib2.roi.labeling; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.Arrays; @@ -174,4 +175,14 @@ public void testHashCodeAndEquals() assertTrue( pixel.equals( expected ) ); assertEquals( expected.hashCode(), pixel.hashCode() ); } + + @Test + public void testGetType() + { + ImgLabeling< String, IntType > imgLabeling = new ImgLabeling<>( ArrayImgs.ints( 10, 10 ) ); + LabelingType< String > type = imgLabeling.getType().createVariable(); + type.clear(); + type.add( "A" ); + assertEquals( Collections.singleton( "A" ), type ); + } }