在尝试使用处理库绘制球体时,我在线程“动画线程”中收到NullPointerException。如果我在Main.java类中声明球体,我可以很好地绘制球体,但是当我尝试将声明移动到另一个类并实例化该类时,我得到一个空指针异常。
Exception in thread "Animation Thread" 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 unTextured3DShapes.Spheres.update(Spheres.java:43)
at main.ShapeDefs.update(ShapeDefs.java:52)
at main.Main.draw(Main.java:37)
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$500(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)
package main;
import peasy.PeasyCam;
import processing.core.PApplet;
import unTextured3DShapes.Spheres;
@SuppressWarnings("serial")
public class Main extends PApplet {
PeasyCam cam;
public Spheres untexturedSphere = new Spheres(0,0,0,100,10,0,0,0,this);
// ShapeDef definition to be updated later
// ShapeDefs shapeDefinitions = new ShapeDefs();
public void setup() {
size(displayWidth, displayHeight, OPENGL);
cam = new PeasyCam(this, 100);
cam.setMinimumDistance(-width);
cam.setMaximumDistance(width);
// Drawing the images onto the spheres
// untexturedSphere.sphereList.add(new Spheres(10,50,0,100,10,0,0,0,this));
shapeDefinitions.shapeAdditions(this);
}
public void draw() {
// Basic startup parameters
translate(width / 2, height / 2, 0);
background(map(mouseX, -width, width, 0, 128), map(mouseX, -width, width, 128, 255), map(mouseX, -width, width, 50, 200));
smooth(4);
//Calling the Update Methods
// untexturedSphere.update();
shapeDefinitions.update();
}
}
package unTextured3DShapes;
import java.util.ArrayList;
import main.Main;
import processing.core.PApplet;
public class Spheres {
public float x;
public float y;
public float z;
public int r;
public int d;
public float rotx;
public float roty;
public float rotz;
public float dx = 0;
public float dy = 1;
public boolean drawSphere,rotateX,rotateY,rotateZ = false;
PApplet applet = new PApplet();
public ArrayList<Spheres> sphereList = new ArrayList<>();
public Spheres(PApplet p) {
this.applet = p;
}
public Spheres(float xpos, float ypos, float zpos, int rad, int det, float rxval, float ryval, float rzval, Main app) {
x = xpos; y = ypos; z = zpos; r = rad; d = det; rotx = rxval; roty = ryval; rotz = rzval; applet = app;
}
public void crashCheck() {
if (r <=0 || d <= 0 ) { drawSphere = false; } else { if (r > 0 && d > 0) { drawSphere = true; }}
if (rotx >= 0) { rotateX = false; } else { rotateX = true; }
if (roty >= 0) { rotateY = false; } else { rotateY = true; }
if (rotz >= 0) { rotateZ = false; } else { rotateZ = true; }
}
public void update() {
crashCheck();
applet.pushMatrix();
applet.translate(x, y, z);
if (drawSphere = false) { ; } else {applet.noFill(); applet.sphereDetail(d); applet.sphere(r); }
if (rotateX = false) { ; } else { applet.rotateX(rotx); }
if (rotateY = false) { ; } else { applet.rotateY(roty); }
if (rotateZ = false) { ; } else { applet.rotateZ(rotz); }
applet.popMatrix();
// Gravity and Physics
// At some point in the future, make the gravity and other physics
// dynamic based off of size, etc.
x += dx;
y += dy;
dy += 1;
if (x > applet.width/2 - r || x < applet.width/ -2 + r) { //If "x" is greater than "width" or less than "0", then "dx" times negative 1
dx *= -1; //Multiply dx (delta speed) by negative 1
}
if (y > applet.height/2 - r || y < applet.height / -2 + r) {
dy *= -0.9;
}
}
}
package main;
import unTextured3DShapes.Spheres;
public class ShapeDefs {
Main applet;
// Variables for untextured three dimensional shapes
// Variables for Spheres
// float sx = applet.random(-applet.width / +2, applet.width / 2);
// float sy = applet.random(-applet.width / +2, applet.width / 2);
// float sz = 0;
// float srx = sx;
// float sry = sy;
// float srz = 0;
// int srad = (int) applet.sin(applet.random(0, applet.width / 2));
// int sdet = (int) 15;
// float srxval = (applet.radians(applet.frameCount * 3));
// float sryval = (applet.radians(applet.frameCount * 3));
// float srzval = 0;
//
// // Variables for Cubes
// float cx = applet.random(-applet.width / +2, applet.width / 2);
// float cy = applet.random(-applet.width / +2, applet.width / 2);
// float cz = 0;
// int crad = (int) Math.sin(applet.random(0, applet.width / 2));
// int cdet = (int) 15;
// float crxval = applet.radians(applet.frameCount * 2);
// float cryval = applet.radians(applet.frameCount * 2);
// float crzval = 0;
// Ellipsoid e = new Ellipsoid(this, 2, 50);
// Background definition
// Rendering Back1 = new Rendering();
// Untextured Three Dimensional shape definitions
// Sphere Definitions
Spheres utSpheres = new Spheres(this.applet);
// Cube Definitions
//Cubes C1 = new Cubes(cx, cy, cz, 100, cdet, crxval, cryval, crzval, this.applet);
public void shapeAdditions(Main app) {
// utSpheres.sphereList.add(new Spheres(sx, sy, sz, srad, sdet, srxval, sryval, srzval, this.applet));
utSpheres.sphereList.add(new Spheres(0,0,0,100,10,0,0,0,this.applet));
}
public void update() {
// Call to update method for three dimensional shapes
utSpheres.update();
// C1.update();
// e.draw();
}
}