【问题标题】:lerpColor() / fill() throwing NullPointerException with ProcessinglerpColor() / fill() 使用处理抛出 NullPointerException
【发布时间】:2012-10-23 07:04:02
【问题描述】:

这是一个类的代码:

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 中使用带有 proclipsing 的处理。)

【问题讨论】:

  • @Petros 是对的——除非这是你的主类,否则你不应该扩展 PApplet。似乎不是,因为您是从其他地方致电new Circle()。也就是说,c 不能是 null,因为 null 在 Java 中不是 int 的有效值。这里肯定发生了其他事情。你试过println(c)吗?你得到了什么价值?
  • @ericsoco,我已经检查过 c 的值是否正确,并且确实如此。正如我在 Petros 的回答中所说,在父小程序上调用 fill() 有效。谢谢。

标签: colors processing


【解决方案1】:

首先我不完全确定您为什么需要从一个似乎不是您的窗口之一的类中扩展 PApplet,但我离题了。

如果我了解您要执行的操作,则问题出在填充功能而不是 lerpColor。如果你试图调用你的主 PApplet 的填充函数并且这个 Circle 类不是它,那么你需要告诉它在哪个 PApplet 上调用它。即您已经发送的父母。我会做这样的事情。

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

【讨论】:

  • 我正在扩展 PApplet,因为我需要扩展它才能引用 fill()。我显然使用了错误的,因为在父 PApplet 上调用它有效。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-26
  • 2017-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-16
  • 2015-12-14
相关资源
最近更新 更多