我无法让一些代码在 Eclipse 中工作。它是带有来自 Processing 的元素的 Java 代码,它在 Processing IDE 中工作,但在 Eclipse 中会立即出现空指针异常。它还涉及另一个名为 Picking 的库。我有其他处理代码在 Eclipse 中运行良好,所以我怀疑它与 Picking 库有关。
Picking jar 在我的构建路径上,根据堆栈跟踪,我认为它可以正常访问。
这是堆栈跟踪:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at processing.opengl.PJOGL.enable(PJOGL.java:1640)
at processing.opengl.PGraphicsOpenGL.blendModeImpl(PGraphicsOpenGL.java:5947)
at processing.core.PGraphics.blendMode(PGraphics.java:1842)
at processing.core.PGraphics.defaultSettings(PGraphics.java:952)
at processing.opengl.PGraphicsOpenGL.defaultSettings(PGraphicsOpenGL.java:2003)
at processing.core.PGraphics.checkSettings(PGraphics.java:892)
at picking.Buffer.callCheckSettings(Unknown Source)
at picking.Picker.<init>(Unknown Source)
at PickingDemo.setup(PickingDemo.java:29)
at processing.core.PApplet.handleDraw(PApplet.java:2281)
at processing.opengl.PJOGL$PGLListener.display(PJOGL.java:862)
at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:652)
at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:636)
at javax.media.opengl.awt.GLCanvas$10.run(GLCanvas.java:1284)
at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1106)
at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:981)
at javax.media.opengl.awt.GLCanvas$11.run(GLCanvas.java:1295)
at javax.media.opengl.Threading.invoke(Threading.java:193)
at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:541)
at javax.media.opengl.awt.GLCanvas.paint(GLCanvas.java:595)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:308)
at java.awt.Component.dispatchEventImpl(Component.java:4736)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:647)
at java.awt.EventQueue$3.run(EventQueue.java:645)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
这是代码:
import picking.*;
import processing.core.*;
public class PickingDemo extends PApplet{
// Declare a Picker object to do the picking.
Picker picker;
// Set up variables for the scene.
float r = (float) 0.0;
int[] colors = {
color(064,064,064), // Black
color(255,000,000), // Red
color(000,255,000), // Green
color(000,000,255), // Blue
color(255,255,000), // Yellow
color(255,000,255), // Purple
color(000,255,255), // Cyan
color(255,255,255) }; // White
int bgcolor = color(32);
public void setup(){
// Set the size, and make this a 3D sketch.
size(300,300, P3D);
// Set up the picking object.
picker = new Picker(this);
}
public void draw(){
// Define a variable to track the current object's ID.
int currentID = 0;
// The background's ID is always -1
background(bgcolor);
lights();
noStroke();
translate((float) (width/2.0), (float) (height/2.0), (float) (30));
rotateY(r);
r= (float) ((r+0.01)%TWO_PI);
for(int x=-1;x<2;x++){
for(int y=-1;y<2;y++){
for(int z=-1;z<2;z++){
// Each box has a different object ID, so you have to tell the picker which ID to use.
picker.start(currentID);
pushMatrix();
scale(40);
translate(x,y,z);
if(x*y*z != 0){
// Each box is has a different color.
fill(colors[currentID]);
// Draw the box.
box(1);
currentID++;
}
popMatrix();
// Tell the picker to stop using the ID, since we don't want any other objects to use this ID.
picker.stop();
}
}
}
// See the click buffer. Warning: Heavy code.
//loadPixels();
//picker.getBuffer().loadPixels();
//for( int i = 0; i < width * height; i++){
// pixels[i] = 200 * picker.getBuffer().pixels[i];
//}
//updatePixels();
}
public void mouseClicked(){
// Get the object ID at the mouse's position.
int mouseID = picker.get(mouseX,mouseY);
println( mouseID );
if(mouseID!=-1){
// Set the background to the same color (but darker) to indicate which box was clicked.
bgcolor = color( red(colors[mouseID])/2, green(colors[mouseID])/2, blue(colors[mouseID])/2 );
}
}
}
提前感谢您提供的任何帮助。