好吧,我是编程的新手,所以我没有很清楚的基础知识(我的英语也很糟糕)。当我开始使用这段代码时,我希望它能够绘制一些点,让它们保持移动状态,如果它们比它们之间的X更近,那么画一条线。但是我想让它变得有点复杂,然后添加另一个类,它不打算与其他类交互并画一个圆圈,旁边有一些,约束在一个矩形内(我试图将它们限制在一个圆圈内)但是......现在我的脑袋太多了。嗯,问题是我认为代码是“清除”,至少对于PC来说,我不知道为什么我得到这个“java.lang.NullPointerException”错误,因为它没有显示我错误的地方。
非常感谢任何帮助。
//Test nº1. An array of spheres which are randomly moving, connected between them if the distance is less than 50p.
//The spheres' starting position are determined by the red(x) and green(y) color space from colordata from images.
int[] tots = {#2d2a33, #8e8397, #160e19, #8263a9, #0f1723, #737a86, #2d2a33, #8e8497, #15101c, #965555, #1a2940, #9b7873, #080b12, #272b41, #211f33, #8b6659,
#070719, #4c5985, #423444, #9c8696, #1a2d13, #799078, #30160e, #da9c7b, #2c2e3a, #6d798e, #28474f, #b63f10, #040c1a, #cf6c24, #442036, #a87d99,
#19191a, #756d6e, #1a2828, #94b1a2, #060c15, #6d98c1, #284062, #9ab9be, #121720, #abb7c5, #6e6b9b, #282033};
//--Malla--
float xo, yo;
int numMalla = 200;
Malla[] ma = new Malla [numMalla];
float x;
float y;
float angle = 0.0; // Direction of motion
//float speed = random(0.2, 0.8); // Speed of motion
float diameter = 0.0;
//----
//--Errant--
int numPiEr = 10;
int e;
float xoEr = width/2;
float yoEr = height/2;
float xoPiEr = xoEr;
float yoPiEr = yoEr;
float cRotO = 0;
Errant ccEr;
//Errant clEr;
Errant[] piEr = new Errant [numPiEr];
//----
void setup() {
//--Malla--
//coords mapping into the original position
xo = map( (tots[int(random(0,tots.length))]& 0xFF0000)>>16, 0, 255, 0, width);
yo = map( (tots[int(random(0,tots.length))]& 0x00FF00)>>8, 0, 255, 0, height);
//----
size(1024, 768,P2D);
background(0);
smooth();
frameRate(25);
//float rad = random(5.0,150.0);
//--Malla--
for (int i = 0; i < ma.length; i++) {
ma[i] = new Malla(random(10,width-10),random(10,height-10),angle);
}
//----
//--Errant--
Errant ccEr = new Errant (xoEr, yoEr, cRotO);
//Errant clEr = new Errant (xoEr, yoEr, cRotO);
for (int e = 0; e < piEr.length; e++) {
piEr[e] = new Errant(xoPiEr, yoPiEr, cRotO);
}
//----
}
void draw() {
background(0);
for (int i = 0; i < ma.length; i++) {
ma[i].maMove();
for (int j = i + 1; j < ma.length; j++) {
if (ma[i].nearAnotherMalla(ma[j])) {
ma[i].highlight();
}
}
ma[i].display();
}
for (int e = 0; e < piEr.length; e++) {
piEr[e].piErMove();
piEr[e].piErDisplay();
}
ccEr.erMove();
ccEr.errDisplay();
}
class Malla {
float x,y;
float diameter;
float speedX, speedY;
float angle;
//int temps;
//color c = color(255);
Malla[] nearMalla = new Malla[numMalla];
//----
Malla(float xpos, float ypos, float dir) {
xo = x;
yo = y;
x = xpos;
y = ypos;
//speedX = random(-0.2, 0.2);
//speedY = random(-0.2, 0.2);
speedX = random(-2, 2);
speedY = random(-2, 2);
//temps=0;
//diameter = random(5, 13);
diameter = 2.5;
angle = dir;
}
//----
void maMove() {
/* if(temps==10){
speedX = random(-2, 2);
speedY = random(-2, 2);
temps=0;
}
++temps;*/
if(x+diameter >= width+20) {
speedX = -speedX;
}
else if(x+diameter <= -20) {
speedX = -speedX;
}
if(y+diameter >= height+20) {
speedY = -speedY;
}
else if(y+diameter <= -20) {
speedY = -speedY;
}
// angle += random(-0.13, 0.13);
/* x += speedX;
y += speedY;*/
angle += random(-0.13, 0.13);
x += cos(angle) * speedX;
y += sin(angle) * speedY;
}
//----
void highlight() {
//c = color(255,100);
stroke(255,1);
for (int i = 0; i < ma.length; i++) {
if (nearMalla[i] != null) {
line(x, y, nearMalla[i].x, nearMalla[i].y);
}
}
}
//----
void display() {
//stroke(c);
noStroke();
//noFill();
fill(255);
ellipse(x,y,diameter,diameter);
//c = color(0);
}
//----
boolean nearAnotherMalla(Malla b) {
boolean near = dist(x,y,b.x,b.y) <= 60;
for (int i = 0; i < ma.length; i++) {
if (near) {
nearMalla[i] = b;
}
else {
nearMalla[i] = null;
}
}
return near;
}
}
这是不起作用的,单个对象
class Errant {
float xEr, yEr, xPiEr, yPiEr, cRot, velEr, velErX, velErY, velPiEr, velPiErX, velPiErY;
int ccDiam, clDiamX, clDiamY, piErDiam;
Errant[] nearPiEr = new Errant[numPiEr];
//----
Errant(float xEr, float yEr, float cRot) {
ccDiam = 15;
clDiamX = 80;
clDiamY = 70;
xEr = xoEr;
yEr = yoEr;
xPiEr = random((xEr-20),(xEr+20));
yPiEr= random((yEr-20), (yEr+20));
cRot = cRotO;
velEr = random(-3,3);
velEr = velErX = velErY;
velPiEr = random(-1.5,1.5);
velPiEr = velPiErX = velPiErY;
}
void erMove() {
if(xEr+clDiamX >= width+100) {
velErX = -velErX;
}
else if(xEr+clDiamX <= -100) {
velErX = -velErX;
}
if(yEr+clDiamX >= height+100) {
velErY = -velErY;
}
else if(yEr+clDiamX <= -100) {
velErY = -velErY;
}
cRot += random(-0.2,0.2);
xEr = cos(cRot)*velEr;
yEr = sin(cRot)*velEr;
}
void piErMove() {
if(xPiEr >= xEr+clDiamX) {
velErX = -velPiErX;
}
else if(xPiEr <= xEr-clDiamX) {
velErX = -velPiErX;
}
if(yPiEr >= yEr+clDiamY) {
velErY = -velPiErY;
}
else if(yPiEr <= xEr-clDiamY) {
velErY = -velPiErY;
}
xPiEr = cos(cRot)*velPiEr;
yPiEr = sin(cRot)*velPiEr;
}
void errDisplay () {
noStroke();
fill(#EFE4B0, 70);
ellipse(xEr, yEr, 10, 10);
}
void piErDisplay () {
fill(#EFC6B0, 50);
ellipse(xPiEr, yPiEr, 1, 1);
}
}
提前致谢,girald0。
这是我在控制台中收到的错误消息:
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 punts_moviment_mapejats2.draw(punts_moviment_mapejats2.java:111)
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)
答案 0 :(得分:1)
Processing首先运行sketch()方法,然后连续运行draw()方法。如果你不想根据我对你的问题的评论深入挖掘实际的.java源代码,你可以开始在设置和绘图中评论东西,直到你的草图再次运行,这将指向违规行。
问题似乎是第94行:
ccEr.erMove();
ccEr为空。
您可以在第37行定义此变量两次:
Errant ccEr;
再一次在第63行:
Errant ccEr = new Errant (xoEr, yoEr, cRotO);
处理似乎只考虑第一个(第37行),并且在那个实际上你没有创建对象,只是一个名为ccEr的变量,里面没有任何内容(如果你愿意,则为null)< / p>
现在,我评论第37行,然后根本找不到变量!这让我感到困惑,因为我知道你可以定义&#34; global&#34;到处变量。显然,Processing的内部hocus-pocus已经改变,因此您必须在设置()...上传输第63行(您定义和创建ccEr的位置)...