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 '...

问题:

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.r.g.b.a(DefaultLookAndFeel.java:907) at com.codename1.r.an.a(TextArea.java:1259) at com.codename1.r.l.J(Component.java:2942) at com.codename1.r.l.K(Component.java:2981) at com.codename1.r.l.aa(Component.java:1340) at com.codename1.r.l.af(Component.java:1430) 03-19 09:08:47.786 22799-22820/? W/System.err: at com.codename1.r.e.a.b(BorderLayout.java:480) at com.codename1.r.n.a(Container.java:2224) at com.codename1.r.l.J(Component.java:2942) at com.codename1.r.l.K(Component.java:2981) at com.codename1.r.l.aa(Component.java:1340) at com.codename1.r.l.af(Component.java:1430) at com.codename1.r.e.b.a(BoxLayout.java:155) at com.codename1.r.n.bP(Container.java:1715) at com.codename1.r.n.K(Container.java:1707) at com.codename1.r.n.bP(Container.java:1720) at com.codename1.r.n.K(Container.java:1707) at com.codename1.r.n.bP(Container.java:1720) at com.codename1.r.n.K(Container.java:1707) at com.codename1.r.n.bP(Container.java:1720) at com.codename1.r.n.K(Container.java:1707) at com.codename1.r.n.a(Container.java:1606) at com.codename1.r.v.a(Form.java:4046) at com.codename1.r.l.b(Component.java:2214) at com.codename1.r.v.b(Form.java:4056) at com.codename1.r.l.d(Component.java:2187) at com.codename1.r.l.a(Component.java:2162) 03-19 09:08:47.787 22799-22820/? W/System.err: at com.codename1.r.l.d(Component.java:2130) at com.codename1.r.l.c(Component.java:2421) at com.codename1.r.l.i(Component.java:2365) at com.codename1.impl.a.s(CodenameOneImplementation.java:613) at com.codename1.r.q.l(Display.java:1161) at com.codename1.r.q.k(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!


回答1:

Updated TextArea is an UI component so all interaction should happen on the AWT 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.

Instead, if you want to interact with Swing components, then you should ensure you are on the EDT. For example:

if(EventQueue.isDispatchThread()) {
    textArea.setText(text);
} else {
    SwingUtilities.invokeLater(() -> textArea.setText(text));
}

回答2:

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.

  • 发表于 2019-03-20 18:40
  • 阅读 ( 199 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除