NullPointerException when work with Leap Motion and Arduino

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:

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);
  }
}

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.

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

And here is the interface of Processing when I run with Leap Motion
Picture2

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.

1 Like