# NullPointer Exception on port.write

**URL:** <https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [January 24, 2019, 9:32pm UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822 "2019-01-24T21:32:43Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrDoggss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrdoggss/32/3409_2.png) [@MrDoggss](https://discourse.processing.org/u/MrDoggss)\
**Post date:** [January 24, 2019, 9:32pm UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822/1 "2019-01-24T21:32:43Z")

</div>

I’m trying to send data to an Arduino, but I keep getting a NullPointerException. I don’t get it. I’m no coder, so I am working with examples. If someone could point me in the right direction that would be awesome!

```auto
import processing.serial.*;

import cc.arduino.*;
int xpos=90; // set x servo's value to mid point (0-180);
int ypos=90; // and the same here
Serial port; // The serial port we will be using
Arduino arduino;

void setup()
{
  size(360, 360);
  frameRate(100);
  println(Serial.list()); // List COM-ports
  //select second com-port from the list (COM3 for my device)
  // You will want to change the [1] to select the correct device
  // Remember the list starts at [0] for the first option.
  arduino = new Arduino(this, Arduino.list()[1], 9600);
}

void draw()
{
  fill(175);
  rect(0,0,360,360);
  fill(255,0,0); //rgb value so RED
  rect(180, 175, mouseX-180, 10); //xpos, ypos, width, height
  fill(0,255,0); // and GREEN
  rect(175, 180, 10, mouseY-180);
  update(mouseX, mouseY);
  
}

void update(int x, int y)
{
  //Calculate servo postion from mouseX
  xpos = x/2;
  ypos = y/2;
  //Output the servo position ( from 0 to 180)
  port.write(xpos+"x");
  port.write(ypos+"y");
}

```

---

<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:** [January 25, 2019, 3:32am UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822/2 "2019-01-25T03:32:52Z")

</div>

in your console you should see a list or line about available ports,  
on windows like COM5  
for your arduino.  
and you should know that number already from the arduino IDE  
so if that comes up first must change in the code to

> [@](#):
>
> Arduino.list()[0]

---

<div class="post-metadata">

**Author:** ![MrDoggss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrdoggss/32/3409_2.png) [@MrDoggss](https://discourse.processing.org/u/MrDoggss)\
**Post date:** [January 25, 2019, 7:14am UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822/3 "2019-01-25T07:14:04Z")

</div>

The Arduino is on COM3, and the only 2 that show up in the Processing console are COM1 and 3. 😕

So is the NullPointer Exception actually a serial error?

---

<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:** [January 26, 2019, 4:34am UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822/4 "2019-01-26T04:34:23Z")

</div>

sorry,  
if the list is

> com1  
> com3

and the arduino is on

> com3

[1]  
is correct setting.

but now i see that you make the variable  
port  
but never define it,  
but try to write to it.  
so not sure about the arduino thing, can not test right now, so pls check on that and find a example for it.

> when i am out of hospital i will check also.\_
> 
> * * *
> 
> _ok, after 3 month working with one eye, yesterday got a second plastic lens_  
> _and now see it all again!_

> [@MrDoggss](#):
>
> I’m no coder, so I am working with examples

that seems to be the point,  
instead try one example and get it running looks like you try to mix two  
-1- example from serial lib  
-2- example from arduino lib  
( what is not a good name as it is for arduino loaded with **firmata** code ONLY )

so actually besides to say again

> just try one example

above code can only be changed to one or the other  
knowing what you want to do?  
spec. if your arduino should run / is loaded / with firmata code /  
like as remote I/O for user who not want to learn arduino coding  
and just use the arduino IDE to upload the firmata…

OR

you have your own code loaded to the arduino what expects the TEXT  
`xpos180 ypos180`

so, please tell us more about your project.

---

<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:** [January 27, 2019, 2:55pm UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822/5 "2019-01-27T14:55:17Z")

</div>

if you really want to use ( no coding arduino side! )

> arduino firmata

i just made a short snapshot gallery [here](http://kll.engineering-news.org/kllfusion01/articles.php?article_id=158)  
incl code example processing ( loopback makes testing more easy )

---

<div class="post-metadata">

**Author:** ![MrDoggss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrdoggss/32/3409_2.png) [@MrDoggss](https://discourse.processing.org/u/MrDoggss)\
**Post date:** [January 27, 2019, 6:19pm UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822/6 "2019-01-27T18:19:54Z")

</div>

I’ve tried several example sketch sets, and several more from users. I’m trying to control a servo using an Arduino and mouse on a PC. I do not want to use Firmata since this is going to be used in a much larger sketch with other features. I am trying to get the basics of how to read and write data over a serial, then use the data for controlling things. The simple sketches I find aren’t what I need, and the more complex ones aren’t working.

Right now, I just want to figure out the basics coding for this setup.

Arduino Mega 2560 R3 on COM3, servo connected to pin 7.  
Use mouse X or Y value sent over serial, to be read by Arduino.(without Firmata)

I’m still trying to learn this on my own, but any info would be great. Thank you, everyone, for the help and comments!

---

<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:** [January 27, 2019, 8:21pm UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822/7 "2019-01-27T20:21:12Z")

</div>

> [@MrDoggss](#):
>
> import cc.arduino.\*;

good, so get rid of the firmata lib

```auto
// import cc.arduino.*;

```

and in case you use the servo what needs  
0 … 127  
better write just

```auto
int(map( mouseX,0,width,0,127))
int(map( mousey,0,height,0,127))

```

in the form of a CSV line

> 127,127,

---

<div class="post-metadata">

**Author:** ![MrDoggss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrdoggss/32/3409_2.png) [@MrDoggss](https://discourse.processing.org/u/MrDoggss)\
**Post date:** [January 29, 2019, 7:30pm UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822/8 "2019-01-29T19:30:08Z")

</div>

I got rid of the Firmata lib, but I’m not sure where the int lines go yet.

I haven’t had much time to work on it, but I will over the next couple days.

---

<div class="post-metadata">

**Author:** ![MrDoggss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrdoggss/32/3409_2.png) [@MrDoggss](https://discourse.processing.org/u/MrDoggss)\
**Post date:** [February 4, 2019, 7:11pm UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822/9 "2019-02-04T19:11:23Z")

</div>

Well, I think I’ve burned myself out. I have worked with this for a few days, and I am still not getting it. Very frustrated right now, and pretty sure my thinking is wrong on how to do this. The examples I try seem to work, but they either use firmata, or use a single piece of data like 0 or 180.

Every time I try to modify, or write a similar code, it doesn’t work. The servo never moves.

Thanks for the help, and if anyone comes across a simple sketch that might work, shoot me a link.

This is my Arduino code. I keep getting the feeling that Processing isn’t sending the data in the right format or something since this works with another sketch, but nothing to back that thought up.

```auto
#include <Servo.h> 

Servo myservo; // create servo object to control a servo 

                // a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position
int ServoPin = 7; //identify which pin the servo is connected to
int ServoAngle; //create a variable

void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);
  myservo.attach(ServoPin); // attaches the servo on pin 9 to the servo object
}

void loop() {
  if (Serial.available() > 0) {
    // read the incoming byte:
    ServoAngle = Serial.read(); //read the angle that was sent from the Processing sketch
    myservo.write(ServoAngle); // move the servo to the new angle
  }
}

```

---

<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:** [February 5, 2019, 3:09am UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822/10 "2019-02-05T03:09:55Z")

</div>

//\_\_\_ hardware test  
i know your arduino code looks like,  
but sure you tested first the excample  
[https://www.arduino.cc/en/Tutorial/Knob](https://www.arduino.cc/en/Tutorial/Knob)  
so you tested your wiring ( ok on pin 7 not 9 )

//\_\_\_ arduino processing  
you send data from 0 … 180 ?

now whenever you do something in **arduino** you could  
send feedback, some info like

```auto
Serial.print("set angle: ");
Serial.println(ServoAngle);

```

(if you test at terminal/monitor see

```auto
1[enter]
set angle: 49 // character 1 as int
set angle: 10 // from the enter

```

at the same time in **processing** can catch that

```auto

void serialEvent(Serial myPort) { // if want to see what the arduino is answering:
  String arduinoline = myPort.readStringUntil(10);
  if ( arduinoline != null ) print(arduinoline);
}

```

//\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ update  
processing code:

```auto
import processing.serial.*;
Serial arduinoPort;

boolean diag = true;//false;//true; // print / echo diagnostic

void setup() {
  printArray(Serial.list());
  arduinoPort = new Serial(this, "/dev/ttyACM0", 9600 );
  println("use: key [s] send random angle 0 .. 180");
}

void draw() {
}

void send_setpoint() {
  int outi;
  byte outb;
  outi = int(random(181));
  outb = byte(outi);
  if (diag) println("send to arduino: i "+outi+" b "+outb);
  arduinoPort.write(outb);
}

void keyPressed() {
  if ( key == 's') send_setpoint();
}

void serialEvent(Serial myPort) { // if want to see what the arduino is answering:
  String arduinoline = myPort.readStringUntil(10);
  if ( arduinoline != null ) print(arduinoline);
}

```

arduino code:

```auto
#include <Servo.h>

Servo myservo; // create servo object to control a servo, maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position
int ServoPin = 7; //identify which pin the servo is connected to
int ServoAngle; //create a variable

void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);
  while (!Serial) { ; } // for LEONARDO needed
  Serial.println("<Arduino is ready>");

  myservo.attach(ServoPin); // attaches the servo on pin 9 to the servo object
}

void loop() {
  if (Serial.available() > 0) {
    // read the incoming byte:
    ServoAngle = Serial.read(); //read the angle that was sent from the Processing sketch
    myservo.write(ServoAngle); // move the servo to the new angle
    Serial.print("set angle: ");
    Serial.println(ServoAngle);
  }
}

```

! not tested with a servo !

---

<div class="post-metadata">

**Author:** ![MrDoggss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrdoggss/32/3409_2.png) [@MrDoggss](https://discourse.processing.org/u/MrDoggss)\
**Post date:** [February 21, 2019, 7:13pm UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822/11 "2019-02-21T19:13:52Z")

</div>

Sorry for the long delay. Your code works perfectly with the servo. I just changed the serial port to “COM3”. The serial data comes back and the servo move to the proper angle.

So, now I know that it can work. I’ll try to modify your code to be able to input a specific number.

I’ll post once I see where I can go with this.

Thank you!!!

---

<div class="post-metadata">

**Author:** ![MrDoggss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrdoggss/32/3409_2.png) [@MrDoggss](https://discourse.processing.org/u/MrDoggss)\
**Post date:** [February 21, 2019, 8:26pm UTC](https://discourse.processing.org/t/nullpointer-exception-on-port-write/7822/12 "2019-02-21T20:26:06Z")

</div>

Ok, so I changed the random number to mouseX, and it works as long as I keep the S key pressed. All good there. I changed the Arduino code to incorporate a second servo (since I need to be able to do multiple servos) and started to change the Processing code to have a second value based on mouseY.

That’s where I get lost. I can’t figure out how the arduinoPort.write and the read on the Arduino end get split for 2 values. I’m reading on strings and serial data right now.

Processing code

```auto
import processing.serial.*;
Serial arduinoPort;

boolean diag = true;//false;//true; // print / echo diagnostic
boolean diag1 = true;//false;//true; 

void setup() {
  printArray(Serial.list());
  arduinoPort = new Serial(this, "COM3", 9600 );
  println("use: key [s] send random angle 0 .. 180");
}

void draw() {
}

void send_setpoint() {
  int outi;
  byte outb;
  outi = int(mouseX);
  outb = byte(outi);
  if (diag) println("send to arduino: i "+outi+" b "+outb);
  arduinoPort.write(outb);
  
  int outj;
  byte outc;
  outj = int(mouseY);
  outc = byte(outj);
  if (diag1) println("send to arduino: j "+outj+" b "+outc);
  arduinoPort.write(outc);
}

void data_send() {
 if ( key == 's') send_setpoint();
 }

void serialEvent(Serial myPort) { // if want to see what the arduino is answering:
  String arduinoline = myPort.readStringUntil(10);
  if ( arduinoline != null ) print(arduinoline);
}

```

Arduino code

```auto
#include <Servo.h>

Servo myservo; // create servo object to control a servo, maximum of eight servo objects can be created
Servo myservo1; // create servo object to control a servo, maximum of eight servo objects can be created

int posa = 0; // variable to store the servo position
int ServoPina = 7; //identify which pin the servo is connected to
int ServoAnglea; //create a variable
int posb = 0; // variable to store the servo position
int ServoPinb = 8; //identify which pin the servo is connected to
int ServoAngleb; //create a variable

void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);
  while (!Serial) { ; } // for LEONARDO needed
  Serial.println("<Arduino is ready>");

  myservo.attach(ServoPina); // attaches the servo on pin 9 to the servo object
  myservo1.attach(ServoPinb); // attaches the servo on pin 9 to the servo object
}

void loop() {
  if (Serial.available() > 0) {
    // read the incoming byte:
    ServoAnglea = Serial.read(); //read the angle that was sent from the Processing sketch
    myservo.write(ServoAnglea); // move the servo to the new angle
    myservo1.write(ServoAngleb); // move the servo to the new angle
    Serial.print("set angle: ");
    Serial.println(ServoAnglea);
    Serial.println(ServoAngleb);
  }
}

```
