Colors lerpColor()/fill()在处理时引发NullPointerException
这是类的代码:Colors lerpColor()/fill()在处理时引发NullPointerException,colors,processing,Colors,Processing,这是类的代码: class Circle extends PApplet { //var declarations Circle(int duration, int from, int to, PApplet parent, int x, int y, int length, int height){ this.ani = new Tween(parent, 1.3f, Tween.SECONDS, Shaper.QUADRATIC); Class s = Shaper
class Circle extends PApplet {
//var declarations
Circle(int duration, int from, int to, PApplet parent, int x, int y, int length, int height){
this.ani = new Tween(parent, 1.3f, Tween.SECONDS, Shaper.QUADRATIC);
Class s = Shaper.QUADRATIC;
this.from = from;
this.to = to;
this.len = length;
this.height = height;
this.x = x;
this.y = y;
}
void update(){
int c = lerpColor(this.from, this.to, this.ani.position(), RGB);
fill(c);
ellipse(this.x, this.y, this.len, this.height);
}
}
当我在正确种子版本的Circle
上运行update()
Exception in thread "Animation Thread" java.lang.NullPointerException
at processing.core.PApplet.fill(PApplet.java:13540)
at ellipses.Circle.update(Ellipses.java:85)
at ellipses.Ellipses.draw(Ellipses.java:39)
at processing.core.PApplet.handleDraw(PApplet.java:2128)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:190)
at processing.core.PApplet.run(PApplet.java:2006)
at java.lang.Thread.run(Thread.java:662)
它告诉我,在fill()
中,一些不应该为空的东西是空的。首先,我假设传递给fill()
的值在某种程度上是错误的。fill()
的值来自lerpColor()
,因此我可能错误地使用了lerpColor()
我的Circle
实例如下所示:
int c1 = color(45, 210, 240);
int c2 = color(135, 130, 195);
cir = new Circle(1, c1, c2, this, 100, 200, 140, 140);
cir.update();
那么如何正确使用fill()
/lerpColor
(顺便说一句,我在eclipse中使用的是带进程的处理。)首先,我不完全确定为什么需要从一个似乎不是您的窗口的类扩展PApplet,但我离题了
如果我理解你想做什么,问题在于fill函数,而不是lerpColor。如果您试图调用主PApplet的fill函数,而这个Circle类不是它,那么您需要告诉它在哪个PApplet上调用它。i、 e.您已经发送的家长。我会这样做
class Circle extends PApplet {
//var declarations
Tween ani;
int from, to, x, y, len, heightz;
PApplet parr; // prepare to accept the parent instance
Circle(int duration, int from, int to, PApplet parent, int x, int y, int length, int height) {
this.ani = new Tween(parent, 1.3f, Tween.SECONDS, Shaper.QUADRATIC);
Class s = Shaper.QUADRATIC;
this.from = from;
this.to = to;
this.len = length;
this.heightz = height;
this.x = x;
this.y = y;
parr = parent; // store the parent instance
}
void update() {
color c = lerpColor(this.from, this.to, this.ani.position(), RGB);
parr.fill(c); // call fill on the parent
parr.ellipse(this.x, this.y, this.len, this.height); // this one obviously suffers from the same problem...
}
我希望这有帮助!
pk@Petros是对的——除非这是你的主课,否则你不应该扩展PApplet。似乎不是,因为您正在从其他地方调用newcircle()
。也就是说,c
不能是null
,因为null
在Java中不是int
的有效值。这里一定发生了什么事。您是否尝试过println(c)
?你得到了什么值?@ericsoco,我检查了c是否有正确的值,结果是。正如我在Petros的回答中所说的,在父小程序上调用fill()
。谢谢。我正在扩展PApplet,因为我需要扩展它,以便能够引用fill()
。很明显,我用错了,因为用父母的名字叫它帕普莱管用。谢谢