# How use Serial object inside a class

**URL:** <https://discourse.processing.org/t/how-use-serial-object-inside-a-class/43568>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [December 27, 2023, 9:12pm UTC](https://discourse.processing.org/t/how-use-serial-object-inside-a-class/43568 "2023-12-27T21:12:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bask185](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@bask185](https://discourse.processing.org/u/bask185)\
**Post date:** [December 27, 2023, 9:12pm UTC](https://discourse.processing.org/t/how-use-serial-object-inside-a-class/43568/1 "2023-12-27T21:12:16Z")

</div>

I had myself a fine working program. But I had to refactor some things. I added a class and inside that class I would like all to do all the serial data handling.

I only have no clue how java works with this. I keep getting null pointer exceptions everywhere and I need some help.

I have this class with a private Serial object. Must it be private, I don’t really know.

```auto
public class LocoNet 
{
    private Serial serial ;

```

Several of this class’ methods do something like `serial.write()`, `serial.read()` etc

one such a method

```auto
    public void sendMessage()
    {
        byte len = getLen( messageOUT[0], messageOUT[1] ) ;

        for( byte i = 0 ; i < len ; i ++ )
        {
            try
            {
                serial.write( messageOUT[i] ) ;
            }
            catch( NullPointerException e)
            {
                NO_COM = true ;
            }
        }
    }

```

All serial related things are in catch blocks. In the event the USB cable is yanked out, the program will not crash completely.

In an attempt to… get it compilable at the very least. I tried making a serial object in the main file and initialize the port in `void setup()` like one normally do.

And I try to pass on the serial object via the constructor.

```auto
    public LocoNet ( Serial s)
    {
        serial = s ;
    }

// main
void setup()
{
    try
    {
        s = new Serial(this, "COM4", 19200) ;
        println("COM PORT OPENED");
        loconet = new LocoNet( s ) ; 
    }
    catch (Exception e)
    {
        println("COM PORT FAILURE");
    }

```

It all doesnt work and all. And I don’t know how to continue.

I would like to know what I am doing wrong, how I must correctly pass on an object (or a pointer to that objec) and I want to know all steps I need to take so I can safely use the serial object inside the class methods.

Kind regards 🥃

Bas

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 28, 2023, 8:46am UTC](https://discourse.processing.org/t/how-use-serial-object-inside-a-class/43568/2 "2023-12-28T08:46:14Z")

</div>

During debugging you should not use try…

because you can’t debug really.

* * *

This

s = new Serial(this, “COM4”, 19200) ;

should work in setup ().

It won’t work in the class though, since “this” is not  
referring to the papplet but to the class.

As a workaround we usually pass “this” to the class constructor so that the class has the PApplet inside it.

So start of class

PApplet sketch;

constructor:

…(PApplet paIn\_){

sketch=paIn\_;

And later

s = new Serial ( sketch, …

* * *

call the constructor with …(this…

Chrisir

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 28, 2023, 11:33am UTC](https://discourse.processing.org/t/how-use-serial-object-inside-a-class/43568/3 "2023-12-28T11:33:36Z")

</div>

Hello @bask185 ,

I was able to adapt this (both approaches) and use Serial library in place of GIF library:  
[https://forum.processing.org/two/discussion/23440/referencing-to-mainapp-papplet-instead-of-this.html](https://forum.processing.org/two/discussion/23440/referencing-to-mainapp-papplet-instead-of-this.html)

The link to the _eclipse tutorial_ in above is a [dead link](https://en.wikipedia.org/wiki/Link_rot).

The tutorial can be found here:

> <https://github.com/processing/processing-docs/tree/master/content/static/tutorials>
>
> //github.com/processing/processing-docs/tree/master/content/static/tutorials

`:)`

---

<div class="post-metadata">

**Author:** ![bask185](https://avatars.discourse-cdn.com/v4/letter/b/3da27b/32.png) [@bask185](https://discourse.processing.org/u/bask185)\
**Post date:** [January 3, 2024, 6:51pm UTC](https://discourse.processing.org/t/how-use-serial-object-inside-a-class/43568/4 "2024-01-03T18:51:43Z")

</div>

Okay I got it working

```auto
    try
    {
        s = new Serial(this, "COM4", 19200) ;
        println("COM PORT OPENED");
        loconet = new LocoNet( this.s ) ;
    }
    catch (Exception e)
    {
        //NO_COM = true ;    
        println("COM PORT FAILURE");
    }

```

Thnx,

Bas
