简体   繁体   中英

NullPointException from Codename One code

Since a few days ago and out of nowhere, I sometimes get this NullPointException error:

Exception: java.lang.NullPointerException - Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference 03-19 09:08:47.785 22799-22820/? W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference at com.codename1.r.an.cj(TextArea.java:1199) at com.codename1.r.an.ci(TextArea.java:865) at com.codename1.r.an.H(TextArea.java:879) at com.codename1.r.an.F(TextArea.java:824) at com.codename1.rgba(DefaultLookAndFeel.java:907) at com.codename1.r.an.a(TextArea.java:1259) at com.codename1.rlJ(Component.java:2942) at com.codename1.rlK(Component.java:2981) at com.codename1.rlaa(Component.java:1340) at com.codename1.rlaf(Component.java:1430) 03-19 09:08:47.786 22799-22820/? W/System.err: at com.codename1.reab(BorderLayout.java:480) at com.codename1.rna(Container.java:2224) at com.codename1.rlJ(Component.java:2942) at com.codename1.rlK(Component.java:2981) at com.codename1.rlaa(Component.java:1340) at com.codename1.rlaf(Component.java:1430) at com.codename1.reba(BoxLayout.java:155) at com.codename1.rnbP(Container.java:1715) at com.codename1.rnK(Container.java:1707) at com.codename1.rnbP(Container.java:1720) at com.codename1.rnK(Container.java:1707) at com.codename1.rnbP(Container.java:1720) at com.codename1.rnK(Container.java:1707) at com.codename1.rnbP(Container.java:1720) at com.codename1.rnK(Container.java:1707) at com.codename1.rna(Container.java:1606) at com.codename1.rva(Form.java:4046) at com.codename1.rlb(Component.java:2214) at com.codename1.rvb(Form.java:4056) at com.codename1.rld(Component.java:2187) at com.codename1.rla(Component.java:2162) 03-19 09:08:47.787 22799-22820/? W/System.err: at com.codename1.rld(Component.java:2130) at com.codename1.rlc(Component.java:2421) at com.codename1.rli(Component.java:2365) at com.codename1.impl.as(CodenameOneImplementation.java:613) at com.codename1.rql(Display.java:1161) at com.codename1.rqk(Display.java:1070) at com.codename1.r.aj.run(RunnableWrapper.java:120) at com.codename1.impl.b$1.run(CodenameOneThread.java:60) at java.lang.Thread.run(Thread.java:776)

It doesn't come from my code and I have no idea how to resolve this intermittent error.

Please help me!

Updated TextArea is an UI component so all interaction should happen on the codenameOne event dispatch thread (EDT).

In the codenameone master file for TextArea, the variable rowText is declared inside the method and has a call prior, so it's not null. So the culprit appears to be rowStrings.add(rowText) .

The stack trace is reported at: https://github.com/codenameone/CodenameOne/blob/master/CodenameOne/src/com/codename1/ui/TextArea.java#L1199

rowStrings is an instance member, but it's not final or protected with locking.

Shai Almog points out in his answer : any manipulation of TextArea should happen on the EDT. TextArea as a UI component should not need to worry about concurrency.

Most of these issues occur due to race conditions and violations of the EDT . This means you made changes to the UI in a separate thread either manually created or obtained via: timer, network etc.

We provide an EDT violation detection tool in the simulator which you can enable. As you run with this tool enabled it will print the stack traces for suspected violations. Notice that in some cases it can produce "false positives" but it's normally pretty good at such cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM