# NullPointerException when work with Leap Motion and Arduino

**URL:** <https://discourse.processing.org/t/nullpointerexception-when-work-with-leap-motion-and-arduino/28984>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [March 30, 2021, 2:26pm UTC](https://discourse.processing.org/t/nullpointerexception-when-work-with-leap-motion-and-arduino/28984 "2021-03-30T14:26:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Negos](https://avatars.discourse-cdn.com/v4/letter/n/b4bc9f/32.png) [@Negos](https://discourse.processing.org/u/Negos)\
**Post date:** [March 30, 2021, 2:26pm UTC](https://discourse.processing.org/t/nullpointerexception-when-work-with-leap-motion-and-arduino/28984/1 "2021-03-30T14:26:42Z")

</div>

I write the code to collect the data of Leap Motion, but show me a NullPointerException (at line 27).  
I’m just a beginner in Processing, What can I do to solve this problem? Please help me.  
This is coding:

```auto
import processing.serial.*;
import de.voidplus.leapmotion.*;

LeapMotion leap;
Serial myPort;
//PVector[] thumbMcp = new PVector[10];

void setup() {
  size(600, 600);
  background(255);
  leap = new LeapMotion(this);
  smooth(8);
  noStroke();
  println((Object[])Serial.list());
  myPort = new Serial(this,"COM3", 57600);
}
void draw() {

  background(255);
  int fps = leap.getFrameRate();
  fill(#00E310);
  text(fps + "fps",20,20);
  for (Hand hand : leap.getHands()) {
    hand.draw();
    pushMatrix();
    //MCP
    PVector thumbMcp = hand.getThumb().getRawPositionOfJointMcp();
    PVector indexMcp = hand.getIndexFinger().getRawPositionOfJointMcp();
    PVector middleMcp = hand.getMiddleFinger().getRawPositionOfJointMcp();
    PVector ringMcp = hand.getRingFinger().getRawPositionOfJointMcp();
    PVector pinkyMcp = hand.getPinkyFinger().getRawPositionOfJointMcp();

    float[] tmcp = thumbMcp.array();
    float[] imcp = indexMcp.array();
    float[] mmcp = middleMcp.array();
    float[] rmcp = ringMcp.array();
    float[] pmcp = pinkyMcp.array();

    //DIP
    PVector thumbDip = hand.getThumb().getRawPositionOfJointDip();
    PVector indexDip = hand.getIndexFinger().getRawPositionOfJointDip();
    PVector middleDip = hand.getMiddleFinger().getRawPositionOfJointDip();
    PVector ringDip = hand.getRingFinger().getRawPositionOfJointDip();
    PVector pinkyDip = hand.getPinkyFinger().getRawPositionOfJointDip();

    float[] tdip = thumbDip.array();
    float[] idip = indexDip.array();
    float[] mdip = middleDip.array();
    float[] rdip = ringDip.array();
    float[] pdip = pinkyDip.array();

    //PIP
    PVector thumbPip = hand.getThumb().getRawPositionOfJointPip();
    PVector indexPip = hand.getIndexFinger().getRawPositionOfJointPip();
    PVector middlePip = hand.getMiddleFinger().getRawPositionOfJointPip();
    PVector ringPip = hand.getRingFinger().getRawPositionOfJointPip();
    PVector pinkyPip = hand.getPinkyFinger().getRawPositionOfJointPip();

    float[] tpip = thumbPip.array();
    float[] ipip = indexPip.array();
    float[] mpip = middlePip.array();
    float[] rpip = ringPip.array();
    float[] ppip = pinkyPip.array();

    //==============================//

    int [] tMcp = int(tmcp);
    int [] iMcp = int(imcp);
    int [] mMcp = int(mmcp);
    int [] rMcp = int(rmcp);
    int [] pMcp = int(pmcp);

    int [] tDip = int(tdip);
    int [] iDip = int(idip);
    int [] mDip = int(mdip);
    int [] rDip = int(rdip);
    int [] pDip = int(pdip);

    int [] tPip = int(tpip);
    int [] iPip = int(ipip);
    int [] mPip = int(mpip);
    int [] rPip = int(rpip);
    int [] pPip = int(ppip);    

    popMatrix();
    //myPort.write(byte(mmcp)); 
    /*println(mMcp[0]);
     println(mMcp[1]);
     println(mMcp[2]);*/

    String Tmcp = "";
    for (int i = 0; i<3; i++) {
      if (i>0) {
        Tmcp += ",";
      }
      Tmcp += tMcp[i];
    }
    String Imcp = "";
    for (int i = 0; i<3; i++) {
      if (i>0) {
        Imcp += ",";
      }
      Imcp += iMcp[i];
    }
    String Mmcp = "";
    for (int i = 0; i<3; i++) {
      if (i>0) {
        Mmcp += ",";
      }
      Mmcp += mMcp[i];
    }
    String Rmcp = "";
    for (int i = 0; i<3; i++) {
      if (i>0) {
        Rmcp += ",";
      }
      Rmcp += rMcp[i];
    }
    String Pmcp = "";
    for (int i = 0; i<3; i++) {
      if (i>0) {
        Pmcp += ",";
      }
      Pmcp += pMcp[i];
    }
    
    println(Tmcp,";",Imcp,";",Mmcp,";",Rmcp,";",Pmcp,"\n");
    myPort.write(Tmcp);
    myPort.write(Imcp);
    myPort.write(Mmcp);
    myPort.write(Rmcp);
    myPort.write(Pmcp);
  }
}

```

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [March 30, 2021, 10:51pm UTC](https://discourse.processing.org/t/nullpointerexception-when-work-with-leap-motion-and-arduino/28984/2 "2021-03-30T22:51:29Z")

</div>

Hi @Negos. I don’t know anything about leap, but I installed the library and ran your sketch. I get a white window with 0fps in green, and no crash!

What device does leap use for input? - touch screen? some device connected to serial? If serial you probably can’t use your own serial to that device as well.

---

<div class="post-metadata">

**Author:** ![Negos](https://avatars.discourse-cdn.com/v4/letter/n/b4bc9f/32.png) [@Negos](https://discourse.processing.org/u/Negos)\
**Post date:** [March 31, 2021, 5:50am UTC](https://discourse.processing.org/t/nullpointerexception-when-work-with-leap-motion-and-arduino/28984/3 "2021-03-31T05:50:08Z")

</div>

Hi @RichardDL . Thank you for your reply  
I use Leap Motion for hand tracking movement, I want to collect the finger position from Leap Motion. After I collect data, I will send the coordinate to Arduino.  
Here is the error:  
 ![Picture1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b122a94cb952cfd49b20bac9682c427c9e4c7403.png)

And here is the interface of Processing when I run with Leap Motion  
 ![Picture2](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7a5fb8e15f05b12e70b633ccc399052c6fdeb1f0.png)

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [March 31, 2021, 7:18am UTC](https://discourse.processing.org/t/nullpointerexception-when-work-with-leap-motion-and-arduino/28984/4 "2021-03-31T07:18:11Z")

</div>

As in the guideline it helps to eliminate parts of the code that may be unrelated. It’s important because not many people have both devices you are using, and it’s hard for us to debug without the devices.

In this case, I would try to first comment out all serial related code and run only with leap motion related code. Then try the same but commenting out leap motion related code so you only have serial related code.

If first (only leap motion code) works but the second (only serial code) doesn’t work, there are issues with the serial. Then try running an example from serial and see if it works. If it works, your code has something wrong. If it doesn’t work, there may be an hardware issue.

If the first doesn’t work and the second works, leap motion related code has issues. Do the same with examples, and in this case assuming from the screen shot that it seems examples work for you, your usage of leap motion library may have a problem.

The trickiest but rare case is both works and only it crashes when they are together. It maybe something is blocking resources of each other and it needs a closer look to see what is the potential problem.
