We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! I have made a program, that has a thread once in 2 seconds writing 5(or more) lines to G4P textarea. To be able to look through those lines I added scrollbars, but when I start to scroll my program gives error:
java.lang.NullPointerException at g4p_controls.StyledString.getLines(Unknown Source) at g4p_controls.GTextArea.appendText(Unknown Source) at CanDecoder.UARTread(CanDecoder.java:54) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at processing.core.PApplet.method(PApplet.java:4167) at processing.core.PApplet$4.run(PApplet.java:4197)
If I move the scrollbar up among writings it crashes with the same error when it comes time to write lines and textarea tries to move cursor to the last line. Any ideas how to fix this? Maybe there is a possibility to stop autoscroll? There is a method to move cursor to the end of text in G4P API(moveCaretEndOfText (TextLayoutHitInfo currPos)), but it requires something I don't understand.
I also had an error writing 5 lines, but it was solved when I added 100ms delay between writings
Thanks in advance Best regards, Paul
Answers
If your program code is short can you post it here or create a simple sketch to demonstrate the error?
main:
gui:
Explanation: Run, press the button, wait for about 7 cycles(14 seconds), move scrollbar to upper position, enjoy:)
If you change the write() and buttonClick() methods to
and
Then 6 lines (with no delay) are appended every time the button is clicked :)
In your original program the statement
thread("write");
causes the write method to be called asynchronously in another thread. It meant that appendText was being called before the last appendText code had finished. That is why adding a 100ms delay worked because it made sure the previous call to appendThread had time to finish.You should not modify any G4P controls in an asynchronous thread.
I write UART messages(from Arduino) to textarea, so clicking all the time wouldn't be a good idea. Of course I knew about asynchronous using of appendText, that’s why I solved this problem quite fast. But taking this loop out of thread to main cycle would slow down GUI. I have thread that appends text to textarea and button that can be pressed any time to change message look before it goes to textarea. So neither moving loop to button event, nor to draw() would be solution for my problem. The only thing left is to stop appending text to read previous messages. Is there a possibility to make scroll panel invisible/visible? I could make button to stop/resume appending text and in this moment scroll panel should appear/hide
The solution is not to use appendText from a secondary thread rather add the new data to the end of a LinkedList.
In the main sketch register the pre() method to read data from the front of the list and append it to the text area.
You need to use a boolean flag to make sure the list is not being read or written to at the same time.
The following sketch demonstrates what I mean in my last comment. Please note no NPE is thrown :-bd
I solved the problem changing delays in writing to timers and putting writing to main cycle. So now program prepares text to write in thread, but writes it to textarea when timer passes 2 seconds, so program doesn't stop for 2 seconds by delay, but works and waits for timer event