处理时的空指针异常

时间:2015-01-17 18:34:25

标签: nullpointerexception arduino processing

有人能帮帮我吗? 我有一个连接到Arduino的陀螺仪发送,以这种方式通过串行输出值:

1;2;3/4;5;6/7;8;9 //new line

该代码用于在序列上获取这些数据,分成3个浮点数组,每个浮点数有3个位置(称为X,Y和Z),它们根据这些值绘制3个轴。

这是我的实际代码:

int radius = 100;

import processing.serial.*;

int lf = 10;  //Linefeed (new line) on ASCII
String myString = null;
Serial myPort;  // Serial port for communication

String[] dataLine;
float[] X;
float[] Y;
float[] Z;

float time, lastTime;

void setup()
{
  size(500, 500, P3D);

  println (Serial.list());  //List avaible serial ports
  myPort = new Serial (this, Serial.list()[0], 9600);  //Assign the first available to the program
  myPort.clear();
  myString = myPort.readStringUntil(lf);  //Read what is on the serial until a new line
  myString = null;  //Throw away what was on the serial 
}

void draw()
{

  background(255);
  pushMatrix();
  translate(radius,radius,radius);

  stroke(255,0,0);
  line(0, 0, 0, X[0], X[1], X[2]);
  stroke(0,255,0);
  line(0, 0, 0, Y[0], Y[1], Y[2]);
  stroke(0,0,255);
  line(0, 0, 0, Z[0], Z[1], Z[2]);
  popMatrix();

  noLoop();
}

void serialEvent (Serial myPort)
{

  if (myPort.available() > 0)
  {
    String myString = myPort.readStringUntil(lf);
    if (myString.length() == 17)
    {
      dataLine = split(myString, '/');

      X = float(split(dataLine[0], ';'));
      print(X[0] + ";" + X[1] + ";" + X[2]);
      println();

      Y = float(split(dataLine[1], ';'));
      print(Y[0] + ";" + Y[1] + ";" + Y[2]);
      println();

      Z = float(split(dataLine[2], ';'));
      print(Z[0] + ";" + Z[1] + ";" + Z[2]);
      println();
    }
  }
  redraw();
}

`

问题是我收到此错误,而不是执行代码:

COM3
java.lang.RuntimeException: java.lang.NullPointerException
    at com.jogamp.common.util.awt.AWTEDTExecutor.invoke(AWTEDTExecutor.java:58)
    at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:103)
    at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:206)
    at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:172)
    at javax.media.opengl.Threading.invoke(Threading.java:191)
    at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:541)
    at processing.opengl.PJOGL.requestDraw(PJOGL.java:688)
    at processing.opengl.PGraphicsOpenGL.requestDraw(PGraphicsOpenGL.java:1651)
    at processing.core.PApplet.run(PApplet.java:2256)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at sketch_DCM_print.draw(sketch_DCM_print.java:53)
    at processing.core.PApplet.handleDraw(PApplet.java:2386)
    at processing.opengl.PJOGL$PGLListener.display(PJOGL.java:862)
    at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:665)
    at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:649)
    at javax.media.opengl.awt.GLCanvas$10.run(GLCanvas.java:1289)
    at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1119)
    at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:994)
    at javax.media.opengl.awt.GLCanvas$11.run(GLCanvas.java:1300)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

当您输入抽奖时,可能的候选人似乎是XYZnull。如果myString.length() != 17,则会发生这种情况。您将它们null保留在串行事件处理程序中,但是在使用阵列时,您不会考虑这种情况。使用调试器。