ACarter
ACarter

Reputation: 5697

lerpColor() / fill() throwing NullPointerException with Processing

This is the code for a class:

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);
  }
}

When I run update() on a properly seeded version of Circle (see below example), I get this stack trace:

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)

It tells me that inside fill(), something is null when it shouldn't be. I would at first assume the value passed to fill() is somehow wrong. The value to fill() comes from lerpColor(), so presumably I've used lerpColor() wrongly.

My instance of Circle looks like this:

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();

So how can I use fill()/lerpColor correctly?

(BTW, I'm using processing in eclipse with proclipsing.)

Upvotes: 0

Views: 1031

Answers (1)

Petros Koutsolampros
Petros Koutsolampros

Reputation: 2800

Well first of all I am not entirely sure why would you need to extend the PApplet from a class that does not seem to be one of your windows, but i digress.

If I understand what you are trying to do, the problem lies with the fill function and not with lerpColor. If you are trying to call the fill function of your main PApplet and this Circle class is not it, then you need to tell it on which PApplet to call it. i.e. the parent that you already send in. I'd do something like this.

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...
  }

I hope this helps! pk

Upvotes: 2

Related Questions