Skip to content

Commit e9df6f7

Browse files
Fix SpotBugs NP_ALWAYS_NULL warnings in Form and UIFragment
Fixes "Null pointer dereference" warnings reported by SpotBugs. - In `Form.java`, fix potential NPE in `getFormLayeredPane` and safely handle `formLayeredPane` using a local variable. - In `UIFragment.java`, initialize `el` before dereferencing it in `buildXMLFromJSONNotation` when the first element is not a String. - Update `.github/scripts/generate-quality-report.py` to fail on `NP_ALWAYS_NULL` violations.
1 parent e1b6c87 commit e9df6f7

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

.github/scripts/generate-quality-report.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ def main() -> None:
758758
spotbugs, _, _ = parse_spotbugs()
759759
if spotbugs:
760760
forbidden_rules = {
761+
"NP_ALWAYS_NULL",
761762
"RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
762763
"RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE",
763764
"UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR",

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,12 +1568,13 @@ public void paintBackgrounds(Graphics g) {
15681568
formLayeredPane.setHeight(getHeight());
15691569
formLayeredPane.setShouldLayout(false);
15701570
}
1571+
Container flp = formLayeredPane;
15711572
if (c == null) {
15721573
// NOTE: We need to use getChildrenAsList(true) rather than simply iterating
15731574
// over layeredPaneImpl because the latter won't find components while an animation
15741575
// is in progress.... We could end up adding a whole bunch of layered panes
15751576
// by accident
1576-
for (Component cmp : formLayeredPane.getChildrenAsList(true)) {
1577+
for (Component cmp : flp.getChildrenAsList(true)) {
15771578
if (cmp.getClientProperty("cn1$_cls") == null) {
15781579
return (Container) cmp;
15791580
}
@@ -1583,16 +1584,20 @@ public void paintBackgrounds(Graphics g) {
15831584
cnt.setWidth(getWidth());
15841585
cnt.setHeight(getHeight());
15851586
cnt.setShouldLayout(false);
1586-
cnt.setName("FormLayer: " + c.getName());
1587-
formLayeredPane.add(cnt);
1587+
if (c != null) {
1588+
cnt.setName("FormLayer: " + c.getName());
1589+
} else {
1590+
cnt.setName("FormLayer: null");
1591+
}
1592+
flp.add(cnt);
15881593
return cnt;
15891594
}
15901595
String n = c.getName();
15911596
// NOTE: We need to use getChildrenAsList(true) rather than simply iterating
15921597
// over layeredPaneImpl because the latter won't find components while an animation
15931598
// is in progress.... We could end up adding a whole bunch of layered panes
15941599
// by accident
1595-
for (Component cmp : formLayeredPane.getChildrenAsList(true)) {
1600+
for (Component cmp : flp.getChildrenAsList(true)) {
15961601
if (n.equals(cmp.getClientProperty("cn1$_cls"))) {
15971602
return (Container) cmp;
15981603
}
@@ -1603,9 +1608,9 @@ public void paintBackgrounds(Graphics g) {
16031608
cnt.setShouldLayout(false);
16041609
cnt.setName("FormLayer: " + c.getName());
16051610
if (top) {
1606-
formLayeredPane.add(cnt);
1611+
flp.add(cnt);
16071612
} else {
1608-
formLayeredPane.addComponent(0, cnt);
1613+
flp.addComponent(0, cnt);
16091614
}
16101615
cnt.putClientProperty("cn1$_cls", n);
16111616
return cnt;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ private static Element buildXMLFromJSONNotation(Object o) throws IOException {
912912
el.addChild(buildXMLFromJSONNotation(first));
913913
}
914914
} else {
915+
el = new Element("flow");
915916
el.addChild(buildXMLFromJSONNotation(first));
916917
}
917918
for (int i = 1; i < len; i++) {

0 commit comments

Comments
 (0)