Hello, sorry for another thread about serialEvent and NullPointerException. I’ve searched for this problem a lot, but actually found no solution to my problem (try/catch just solve the crash not the problem).
import g4p_controls.*;
import processing.serial.*;
import java.util.*;
Serial port;
PrintWriter graph;
PrintWriter triggers;
public void setup()
{
size(470, 350, JAVA2D);
createGUI();
customGUI();
port = new Serial(this,"COM3",500000);
port.bufferUntil('\n');
}
public void draw()
{
background(230);
}
String inString;
void serialEvent(Serial port)
{
try
{
inString = port.readStringUntil('\n');
if (inString == null) { return; }
if (inString.charAt(0) == '\t')
{
println(inString);
}
else if (inString.charAt(0) == 'Q')
{
graph.flush();
graph.close();
triggers.flush();
triggers.close();
}
else if (inString.charAt(0) == '*')
{
triggers.print(inString.replace("*",""));
}
else
{
graph.print(inString);
}
}
catch (Exception e)
{
println("errore");
}
}
The other parts of the code are just GUI stuff.
If I didn’t use the try/catch statement I would get “Disabling serialEvent for COM3 null”, otherwise this is the stack trace:
java.lang.NullPointerException
at GustometroGUI.serialEvent(GustometroGUI.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at processing.serial.Serial.serialEvent(Unknown Source)
at jssc.SerialPort$EventThread.run(SerialPort.java:1112)
The port is initialised in the setup, so why does it say:
at processing.serial.Serial.serialEvent**(Unknown Source)**
Moreover it doesn’t always happen, but if it does once, it will do it again until I restart processing which micht (or not) solve temporarily the problem.
Thank you in advance