How to solve "Disabling serialEvent for COM3 null"

#1

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

1 Like
#2

sounds complicated, never needed,
does this work for you?

void serialEvent(Serial port) {                     // read the serial buffer:
  String myString = port.readStringUntil('\n');
  if ( myString  != null ) {
    myString = trim(myString);                          // clean
    println(myString);
    // split and set some globals...
  }
}

and if we talk arduino board?
https://www.arduino.cc/en/serial/begin
can your board work with

"COM3",500000
#3

does this work for you?

I didn’t understand, what would that code change? It’s actually different than mine.

In your opinion the problem of null pointer is in String or Serial? According to the stack trace it seems to be Serial. So it’s like if serialEvent is called before setup that initialises Serial port.

can your board work with
Absolutely yes. There are many people on arduino forum that claim to have also tried 1Mbaud/s. Moreover I tried that baudrate via PuTTY terminal and just works good

#4

wow, that sounds interesting,
( as some beginner want superfast cryptic byte transfer at 9600 baud. //
but on my level i insist in readable communication //
protocols / stream / i leave to the professionals )

i try to follow you:

  • arduino leonardo

  • 1m usb cable

  • raspberry pi 3B+ // current RASPBIAN

  • arduino ide 1.8.9 hourly

  • processing 3.5.3


arduino code:

String aline ="";  // readable 128 char line

void make_aline() {
aline += "0000000000";
aline += "1111111111";
aline += "2222222222";
aline += "3333333333";
aline += "4444444444";
aline += "5555555555";
aline += "6666666666";
aline += "7777777777";
aline += "8888888888";
aline += "9999999999";
aline += "aaaaaaaaaa";
aline += "bbbbbbbbbb";
aline += "12345678"; 
// ++ CR LF
}

int wait = 500;
long baud = 500000; // 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200, 230400, 250000, 500000, 1000000, 2000000

void setup() {
  Serial.begin(baud);
  make_aline();
  while (!Serial) { ; }                // wait for serial port to connect. Needed for native USB port only ( arduino leonardo )
}

void loop() {
  Serial.println(aline);
  delay(wait);
}

processing code

import processing.serial.*;
Serial port;
String ports, inString;

int baud = 500000; // 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200, 230400, 250000, 500000, 1000000, 2000000
int count=0;

public void setup() {
  size(200, 200);
  printArray(Serial.list());
  ports = Serial.list()[1];
  port = new Serial(this, ports, baud);
  port.bufferUntil('\n');
}

void draw() {
}

void serialEvent(Serial thisport) {                     // read the serial buffer:
  inString = thisport.readStringUntil('\n');
  if ( inString  != null ) {
    inString = trim(inString);                          // clean
    println(count+" : "+inString);
    count++;
    // split and set some globals...
  }
}

no problem
( ok, you are right, 1 000 000 works too )
( and win7/ 64bit also tested )


after my baud rate question back to your

void serialEvent(Serial port)

actually i again avoid your try catch attempt, but i see something
what makes me wonder about a timing conflict

    else if (inString.charAt(0) == 'Q')
    {
      graph.flush();
      graph.close();
      triggers.flush();
      triggers.close();
    }
 

now depending on how big that is what you are doing there
you could test the following:

    else if (inString.charAt(0) == 'Q')
    {
       g_reset = true;
    }

and in draw

 if ( g_reset ) {
      graph.flush();
      graph.close();
      triggers.flush();
      triggers.close();
      g_reset = false;
}

so you avoid that this job is still running ( inside the event )
while the next event is called from port.

1 Like
#5

Thank you very much, by suggesting me to check up those objects I thought that they could be the problem of null pointer. In fact:


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,Serial.list()[0],500000);
  port.bufferUntil('\n');
}

public void draw()
{
  background(230);
}

void serialEvent(Serial port)
{
  String inString = port.readStringUntil('\n');
  if (inString == null) { return; }
  
  if (inString.charAt(0) == '\t')
  {
    println(inString);
  }
  else if  ((triggers != null) && (graph != null)) //this check solves the problem!
  {
    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);
    }
  }
}

And it is solved. Thank you for your time :slight_smile:

2 Likes