Skip to content

Commit 64a2e8e

Browse files
Fix SpotBugs NP_ALWAYS_NULL warnings (#4364)
* 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. * 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` by handling the null case for class `c` correctly and safely handling `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. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent e1b6c87 commit 64a2e8e

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-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: 7 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,16 @@ 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+
cnt.setName("FormLayer: null");
1588+
flp.add(cnt);
15881589
return cnt;
15891590
}
15901591
String n = c.getName();
15911592
// NOTE: We need to use getChildrenAsList(true) rather than simply iterating
15921593
// over layeredPaneImpl because the latter won't find components while an animation
15931594
// is in progress.... We could end up adding a whole bunch of layered panes
15941595
// by accident
1595-
for (Component cmp : formLayeredPane.getChildrenAsList(true)) {
1596+
for (Component cmp : flp.getChildrenAsList(true)) {
15961597
if (n.equals(cmp.getClientProperty("cn1$_cls"))) {
15971598
return (Container) cmp;
15981599
}
@@ -1603,9 +1604,9 @@ public void paintBackgrounds(Graphics g) {
16031604
cnt.setShouldLayout(false);
16041605
cnt.setName("FormLayer: " + c.getName());
16051606
if (top) {
1606-
formLayeredPane.add(cnt);
1607+
flp.add(cnt);
16071608
} else {
1608-
formLayeredPane.addComponent(0, cnt);
1609+
flp.addComponent(0, cnt);
16091610
}
16101611
cnt.putClientProperty("cn1$_cls", n);
16111612
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)