Skip to content

Commit 9ddf708

Browse files
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.
1 parent 79bc59b commit 9ddf708

File tree

8 files changed

+42
-19
lines changed

8 files changed

+42
-19
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,9 @@ public void showPopupDialog(Rectangle rect) {
664664
* This is ignored if there isn't enough space
665665
*/
666666
public void showPopupDialog(Rectangle rect, boolean bias) {
667+
if (rect == null) {
668+
throw new IllegalArgumentException("rect cannot be null");
669+
}
667670
Form f = Display.getInstance().getCurrent();
668671
Rectangle origRect = rect;
669672
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ 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
}
@@ -1442,7 +1442,7 @@ public Container getLayeredPane(Class c, boolean top) {
14421442
// by accident
14431443
java.util.List<Component> children = layeredPaneImpl.getChildrenAsList(true);
14441444
for (Component cmp : children) {
1445-
if (n.equals(cmp.getClientProperty("cn1$_cls"))) {
1445+
if (cmp != null && n.equals(cmp.getClientProperty("cn1$_cls"))) {
14461446
return (Container) cmp;
14471447
}
14481448
}
@@ -1491,7 +1491,7 @@ public Container getLayeredPane(Class c, int zIndex) {
14911491
// is in progress.... We could end up adding a whole bunch of layered panes
14921492
// by accident
14931493
for (Component cmp : layeredPaneImpl.getChildrenAsList(true)) {
1494-
if (cmp.getClientProperty("cn1$_cls") == null) {
1494+
if (cmp != null && cmp.getClientProperty("cn1$_cls") == null) {
14951495
return (Container) cmp;
14961496
}
14971497
}
@@ -1503,7 +1503,7 @@ public Container getLayeredPane(Class c, int zIndex) {
15031503
// by accident
15041504
java.util.List<Component> children = layeredPaneImpl.getChildrenAsList(true);
15051505
for (Component cmp : children) {
1506-
if (n.equals(cmp.getClientProperty("cn1$_cls"))) {
1506+
if (cmp != null && n.equals(cmp.getClientProperty("cn1$_cls"))) {
15071507
return (Container) cmp;
15081508
}
15091509
}

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)