# How to solve "Disabling serialEvent for COM3 null"

**URL:** <https://discourse.processing.org/t/how-to-solve-disabling-serialevent-for-com3-null/10214>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [April 12, 2019, 12:55pm UTC](https://discourse.processing.org/t/how-to-solve-disabling-serialevent-for-com3-null/10214 "2019-04-12T12:55:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank-95](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@Frank-95](https://discourse.processing.org/u/Frank-95)\
**Post date:** [April 12, 2019, 12:55pm UTC](https://discourse.processing.org/t/how-to-solve-disabling-serialevent-for-com3-null/10214/1 "2019-04-12T12:55:04Z")

</div>

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

```auto

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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 12, 2019, 2:12pm UTC](https://discourse.processing.org/t/how-to-solve-disabling-serialevent-for-com3-null/10214/2 "2019-04-12T14:12:00Z")

</div>

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

```auto
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](https://www.arduino.cc/en/serial/begin)  
can your board work with

```auto
"COM3",500000

```

---

<div class="post-metadata">

**Author:** ![Frank-95](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@Frank-95](https://discourse.processing.org/u/Frank-95)\
**Post date:** [April 13, 2019, 9:56am UTC](https://discourse.processing.org/t/how-to-solve-disabling-serialevent-for-com3-null/10214/3 "2019-04-13T09:56:31Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 13, 2019, 10:38am UTC](https://discourse.processing.org/t/how-to-solve-disabling-serialevent-for-com3-null/10214/4 "2019-04-13T10:38:57Z")

</div>

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:

```auto
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

```auto
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

```auto
void serialEvent(Serial port)

```

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

```auto
    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:

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

```

and in draw

```auto
 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.

---

<div class="post-metadata">

**Author:** ![Frank-95](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@Frank-95](https://discourse.processing.org/u/Frank-95)\
**Post date:** [April 13, 2019, 1:16pm UTC](https://discourse.processing.org/t/how-to-solve-disabling-serialevent-for-com3-null/10214/5 "2019-04-13T13:16:21Z")

</div>

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:

```auto

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 🙂
