Skip to content

Commit c113e8a

Browse files
Fix SpotBugs null pointer dereference warnings (#4322)
* Fix SpotBugs null pointer dereference warnings Addressed 12 SpotBugs "Possible null pointer dereference" warnings across 8 files: - CodenameOneImplementation.java: Fixed null usage in captureAudio logging and data check. - InteractionDialog.java: Added null check for rect. - SimpleDateFormat.java: Added return statements to throwInvalid calls. - HashtableContent.java: Added null check for node. - Form.java: Added null checks for components in layered pane iteration. - DefaultLookAndFeel.java: Added null checks for badgeStyle. - SpinnerNode.java: Added null check for selectionListeners before calling isEmpty(). - CommonTransitions.java: Added default case to switch statement. * Fix SpotBugs null pointer dereference warnings Addressed 12 SpotBugs "Possible null pointer dereference" warnings across 8 files: - CodenameOneImplementation.java: Fixed null usage in captureAudio logging and data check. - InteractionDialog.java: Added explicit null checks for Component and Rectangle arguments in showPopupDialog. - SimpleDateFormat.java: Added return statements to throwInvalid calls. - HashtableContent.java: Added null check for node. - Form.java: Added null checks for components in layered pane iteration and safely handled null Class argument in getLayeredPane. - DefaultLookAndFeel.java: Added null checks for badgeStyle. - SpinnerNode.java: Added null check for selectionListeners before calling isEmpty(). - CommonTransitions.java: Added default case to switch statement. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 79bc59b commit c113e8a

File tree

8 files changed

+56
-26
lines changed

8 files changed

+56
-26
lines changed

CodenameOne/src/com/codename1/components/InteractionDialog.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,10 @@ public void showPopupDialog(Component c) {
630630
* This is ignored if there isn't enough space
631631
*/
632632
public void showPopupDialog(Component c, boolean bias) {
633-
Form f = c == null ? null : c.getComponentForm(); // PMD Fix: BrokenNullCheck
633+
if (c == null) {
634+
throw new IllegalArgumentException("Component cannot be null");
635+
}
636+
Form f = c.getComponentForm(); // PMD Fix: BrokenNullCheck
634637
if (f != null && !formMode && !f.getContentPane().contains(c)) {
635638
setFormMode(true);
636639
}
@@ -664,6 +667,9 @@ public void showPopupDialog(Rectangle rect) {
664667
* This is ignored if there isn't enough space
665668
*/
666669
public void showPopupDialog(Rectangle rect, boolean bias) {
670+
if (rect == null) {
671+
throw new IllegalArgumentException("rect cannot be null");
672+
}
667673
Form f = Display.getInstance().getCurrent();
668674
Rectangle origRect = rect;
669675
rect = new Rectangle(rect);

CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5812,7 +5812,7 @@ public void captureAudio(final MediaRecorderBuilder recordingOptions, final com.
58125812
if (!builder.isRedirectToAudioBuffer() && builder.getMimeType() == null) {
58135813
builder.mimeType("audio/wav");
58145814
}
5815-
System.out.println("in captureAudio " + recordingOptions.isRedirectToAudioBuffer());
5815+
System.out.println("in captureAudio " + builder.isRedirectToAudioBuffer());
58165816
final AudioRecorderComponent cmp = new AudioRecorderComponent(builder);
58175817
final Sheet sheet = new Sheet(null, "Record Audio");
58185818
sheet.getContentPane().setLayout(new com.codename1.ui.layouts.BorderLayout());
@@ -5997,8 +5997,10 @@ public void run() {
59975997
Log.e(ex);
59985998
}
59995999
}
6000-
Image im = Image.createImage(data, 0, data.length);
6001-
b.setIcon(im);
6000+
if (data != null) {
6001+
Image im = Image.createImage(data, 0, data.length);
6002+
b.setIcon(im);
6003+
}
60026004
}
60036005
});
60046006

CodenameOne/src/com/codename1/l10n/SimpleDateFormat.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ String readMonth(String source, int ofs, String token, boolean adjacent) {
964964
*/
965965
int parseMonth(String month, int offset) throws ParseException {
966966
if (month == null) {
967-
throwInvalid("month", offset);
967+
return throwInvalid("month", offset);
968968
}
969969
if (month.length() < 3) {
970970
return (parseNumber(month, offset, "month", 1, 12) - 1) + Calendar.JANUARY;
@@ -1066,7 +1066,7 @@ String readTimeZone(String source, int ofs) {
10661066
*/
10671067
int parseTimeZone(String source, int ofs, TimeZoneResult res) throws ParseException {
10681068
if (source == null) {
1069-
throwInvalid("timezone", ofs);
1069+
return throwInvalid("timezone", ofs);
10701070
}
10711071
char tzSign = source.charAt(0);
10721072
if (tzSign == 'z' || tzSign == 'Z') {
@@ -1076,7 +1076,7 @@ int parseTimeZone(String source, int ofs, TimeZoneResult res) throws ParseExcept
10761076
if (tzSign == SIGN_NEGATIVE || tzSign == SIGN_POSITIVE) {
10771077
source = readSubstring(source, 1);
10781078
if (source == null) {
1079-
throwInvalid("timezone", ofs);
1079+
return throwInvalid("timezone", ofs);
10801080
}
10811081
// set the index to point to divider between hours
10821082
// and minutes. Hour can be one or two digits, minutes

CodenameOne/src/com/codename1/processing/HashtableContent.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ public List getChildren(String name) {
197197
}
198198
node = tmp;
199199
}
200+
if (node == null) {
201+
if (oldList) {
202+
return new java.util.Vector();
203+
} else {
204+
return new ArrayList();
205+
}
206+
}
200207
node = ((Map) node).get(name);
201208
if (node == null) {
202209
if (oldList) {

CodenameOne/src/com/codename1/ui/Form.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,20 +1430,22 @@ public Container getLayeredPane(Class c, boolean top) {
14301430
// is in progress.... We could end up adding a whole bunch of layered panes
14311431
// by accident
14321432
for (Component cmp : layeredPaneImpl.getChildrenAsList(true)) {
1433-
if (cmp.getClientProperty("cn1$_cls") == null) {
1433+
if (cmp != null && cmp.getClientProperty("cn1$_cls") == null) {
14341434
return (Container) cmp;
14351435
}
14361436
}
14371437
}
1438-
String n = c.getName();
1438+
String n = c != null ? c.getName() : null;
14391439
// NOTE: We need to use getChildrenAsList(true) rather than simply iterating
14401440
// over layeredPaneImpl because the latter won't find components while an animation
14411441
// is in progress.... We could end up adding a whole bunch of layered panes
14421442
// by accident
14431443
java.util.List<Component> children = layeredPaneImpl.getChildrenAsList(true);
1444-
for (Component cmp : children) {
1445-
if (n.equals(cmp.getClientProperty("cn1$_cls"))) {
1446-
return (Container) cmp;
1444+
if (n != null) {
1445+
for (Component cmp : children) {
1446+
if (cmp != null && n.equals(cmp.getClientProperty("cn1$_cls"))) {
1447+
return (Container) cmp;
1448+
}
14471449
}
14481450
}
14491451

@@ -1491,20 +1493,22 @@ public Container getLayeredPane(Class c, int zIndex) {
14911493
// is in progress.... We could end up adding a whole bunch of layered panes
14921494
// by accident
14931495
for (Component cmp : layeredPaneImpl.getChildrenAsList(true)) {
1494-
if (cmp.getClientProperty("cn1$_cls") == null) {
1496+
if (cmp != null && cmp.getClientProperty("cn1$_cls") == null) {
14951497
return (Container) cmp;
14961498
}
14971499
}
14981500
}
1499-
String n = c.getName();
1501+
String n = c != null ? c.getName() : null;
15001502
// NOTE: We need to use getChildrenAsList(true) rather than simply iterating
15011503
// over layeredPaneImpl because the latter won't find components while an animation
15021504
// is in progress.... We could end up adding a whole bunch of layered panes
15031505
// by accident
15041506
java.util.List<Component> children = layeredPaneImpl.getChildrenAsList(true);
1505-
for (Component cmp : children) {
1506-
if (n.equals(cmp.getClientProperty("cn1$_cls"))) {
1507-
return (Container) cmp;
1507+
if (n != null) {
1508+
for (Component cmp : children) {
1509+
if (cmp != null && n.equals(cmp.getClientProperty("cn1$_cls"))) {
1510+
return (Container) cmp;
1511+
}
15081512
}
15091513
}
15101514

CodenameOne/src/com/codename1/ui/animations/CommonTransitions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,9 @@ public Transition copy(boolean reverse) {
13041304
case TYPE_PULSATE_DIALOG:
13051305
retVal = createDialogPulsate();
13061306
break;
1307+
default:
1308+
retVal = CommonTransitions.createEmpty();
1309+
break;
13071310
}
13081311
retVal.linearMotion = linearMotion;
13091312
return retVal;

CodenameOne/src/com/codename1/ui/plaf/DefaultLookAndFeel.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,17 +1612,25 @@ private void drawComponent(Graphics g, Label l, Image icon, Image stateIcon, int
16121612
g.fillShape(path);
16131613
if (bgColor != strokeColor) {
16141614
g.setColor(strokeColor);
1615-
int alpha2 = g.concatenateAlpha(badgeStyle.getFgAlpha());
1616-
g.drawShape(path, new Stroke(1, Stroke.CAP_SQUARE, Stroke.JOIN_MITER, 1f));
1617-
g.setAlpha(alpha2);
1615+
if (badgeStyle != null) {
1616+
int alpha2 = g.concatenateAlpha(badgeStyle.getFgAlpha());
1617+
g.drawShape(path, new Stroke(1, Stroke.CAP_SQUARE, Stroke.JOIN_MITER, 1f));
1618+
g.setAlpha(alpha2);
1619+
} else {
1620+
g.drawShape(path, new Stroke(1, Stroke.CAP_SQUARE, Stroke.JOIN_MITER, 1f));
1621+
}
16181622
}
16191623

16201624

16211625
g.setColor(fgColor);
16221626
g.setFont(badgeFont);
1623-
int alpha2 = g.concatenateAlpha(badgeStyle.getFgAlpha());
1624-
g.drawString(badgeText, rect.getX() + rect.getWidth() / 2 - badgeTextWidth / 2, rect.getY() + badgePaddingTop);
1625-
g.setAlpha(alpha2);
1627+
if (badgeStyle != null) {
1628+
int alpha2 = g.concatenateAlpha(badgeStyle.getFgAlpha());
1629+
g.drawString(badgeText, rect.getX() + rect.getWidth() / 2 - badgeTextWidth / 2, rect.getY() + badgePaddingTop);
1630+
g.setAlpha(alpha2);
1631+
} else {
1632+
g.drawString(badgeText, rect.getX() + rect.getWidth() / 2 - badgeTextWidth / 2, rect.getY() + badgePaddingTop);
1633+
}
16261634

16271635

16281636
g.setColor(col);

CodenameOne/src/com/codename1/ui/spinner/SpinnerNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ public void addSelectionListener(SelectionListener l) {
239239
public void removeSelectionListener(SelectionListener l) {
240240
if (selectionListeners != null) {
241241
selectionListeners.remove(l);
242-
}
243-
if (selectionListeners.isEmpty()) {
244-
selectionListeners = null;
242+
if (selectionListeners.isEmpty()) {
243+
selectionListeners = null;
244+
}
245245
}
246246
}
247247

0 commit comments

Comments
 (0)