1

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!

Egon Allison
  • 1,329
  • 1
  • 13
  • 22
Julien Sosin
  • 187
  • 8
  • You're using this library: https://github.com/codenameone/CodenameOne check the issues and see if this is reported and whether there's a workaround that exists, otherwise open an issue with them. – Nikos Hidalgo Mar 19 '19 at 09:00
  • Can we see the part of the code where the exception occurs? It does in fact seem to be in your code, when you try to `add` to an `ArrayList`. More precisely, the `ArrayList` seems to be `null`. – Ivan Kukic Mar 19 '19 at 10:56

2 Answers2

1

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.

James
  • 1,095
  • 7
  • 20
  • Ok I will try that ! Thank you James ! – Julien Sosin Mar 20 '19 at 14:46
  • 1
    That answer is incorrect as it references the AWT EDT and not the Codename One EDT which are separate things. It's pretty similar conceptually but the API's are different, SwingUtilities, EventQueue etc. don't exist in Codename One. – Shai Almog Mar 21 '19 at 05:53
1

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.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65