java.lang.RuntimeException:java.lang.NullPointerException PROCESSING

时间:2015-02-04 06:54:15

标签: java nullpointerexception awt processing event-dispatch-thread

所以基本上我试图创建一些复杂的(对于我的编程水平)并尝试使用kinect来触发不同的绘制。 我很抱歉这么多代码,但错误正在慢慢让我疯狂

Car worldmap;
Hotpoint ColorTrigger;

import processing.opengl.*;
import SimpleOpenNI.*;
SimpleOpenNI kinect;

float rotation = 0;
int boxSize = 150;
PVector blueboxCenter = new PVector(200, 800, 600);

PVector boxCenter = new PVector(0, 0, 600);

float s = 1;
void setup() {
  size(displayWidth, displayHeight, OPENGL);

  SimpleOpenNI kinect = new SimpleOpenNI(this);
  kinect.enableDepth();
  ColorTrigger = new Hotpoint(200, 0, 600, 150);
}
void draw() {
  if (keyPressed == true) {
    background(0);
    kinect.update();
    translate(width/2, height/2, -1000);
    rotateX(radians(180));
    translate(0, 0, 1400);
    rotateY(radians(map(mouseX, 0, width, -180, 180)));
    translate(0, 0, s*-1000); 
    scale(s);
    stroke(255);
    PVector[] depthPoints = kinect.depthMapRealWorld();
    int depthPointsInBox = 0;
    for (int i = 0; i < depthPoints.length; i+=10) {
      PVector currentPoint = depthPoints[i];
      point(currentPoint.x, currentPoint.y, currentPoint.z);
      ColorTrigger.check(currentPoint);
    }

    ColorTrigger.display();      

    ColorTrigger.clear() ;
    ColorTrigger.wasJustHit = ColorTrigger.currentlyHit(); 
    ColorTrigger.pointsIncluded = 0;
  } else {
    worldmap.display();
  }
}

    class Hotpoint {
  PVector center;
  color fillColor;
  color strokeColor;
  int size;
  int pointsIncluded;
  int maxPoints;
  boolean wasJustHit;
  int threshold;
  float Gotme;
  Hotpoint(float centerX, float centerY, float centerZ, int boxSize) 
  { 
    center = new PVector(centerX, centerY, centerZ);
    size = boxSize;
    pointsIncluded = 0;
    maxPoints = 1000;
    threshold = 0;
    fillColor = strokeColor = color(random(255), random(255), random(255));
  }
  void setThreshold( int newThreshold ) {
    threshold = newThreshold;
  }
  void setMaxPoints(int newMaxPoints) {
    maxPoints = newMaxPoints;
  }
  void setColor(float red, float blue, float green) {
    fillColor = strokeColor = color(red, blue, green);
  }
  boolean check(PVector point) {
    boolean result = false;
    if (point.x > center.x - size/2 && point.x < center.x + size/2) {
      if (point.y > center.y - size/2 && point.y < center.y + size/2) {
        if (point.z > center.z - size/2 && point.z < center.z + size/2) {
          result = true;
          pointsIncluded++;
        }
      }
    }
    return result;
  }
  void display() {  



    float Gotme = map(pointsIncluded, 0, 1000, 0, 255); 

    translate(center.x, center.y, center.z);
    rotateY(-PI/6);    
    rotateY(radians(frameCount));        
    println(Gotme);

    fill(255, 0, 0, Gotme);
    stroke(255, 0, 0);
    box(boxSize);
  }

  boolean currentlyHit() { 
    return (pointsIncluded > threshold);
  }
  boolean isHit() { 
    return currentlyHit() && !wasJustHit;
  }
  void clear() {
    wasJustHit = currentlyHit(); 
    pointsIncluded = 0;
  }
}
class Car { 
  color c;
  float xpos;
  float ypos;
  float xspeed;

  // The Constructor is defined with arguments.
  Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { 
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
  }

  void display() {
    stroke(0);
    fill(c);
    rectMode(CENTER);
    rect(xpos, ypos, 20, 10);
  }

  void drive() {
    xpos = xpos + xspeed;
    if (xpos > width) {
      xpos = 0;
    }
  }
}

这是我得到的错误

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(Thread.java:745)
Caused by: java.lang.NullPointerException
  at hotbox7.draw(hotbox7.java:73)
  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(InvocationEvent.java:302)
  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
  at java.awt.EventQueue.access$200(EventQueue.java:103)
  at java.awt.EventQueue$3.run(EventQueue.java:694)
  at java.awt.EventQueue$3.run(EventQueue.java:692)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

2 个答案:

答案 0 :(得分:0)

我想,我发现了错误:

首先,您将kinect声明为类属性,然后在kinect - 方法中声明一个新对象setup()。局部变量隐藏了class属性,因此kinect.enableDepth();引用局部变量,而不是class属性。

因此,类属性永远不会被分配(据我所见),所以它仍然是null。调用draw() - 方法时,它会在kinect.update();上失败,因为它引用了class属性null。因为您尝试将方法称为null - 对象,所以Java会抛出NullPointerException

答案 1 :(得分:0)

您已经声明了SimpleOpenNI kinect变量(只是没有初始化为任何内容),所以不要重新在setup()中定义它,只需将其分配给一个值:

void setup() {
  ... 
  kinect = new SimpleOpenNI(this);
  ...
}

通过使用SimpleOpenNI kinect = new SimpleOpenNI(this);,您声明了一个 new 变量,范围为&#34;仅在setup()函数内生存#34;所以当我们退出setup()时,该变量被丢弃,全局kinect仍然是空指针。