0
votes

I've been working on a Processing project recently, and wished to bring it into Eclipse (for use with another class written in Java). Basically what this project is is a race simulation, and I have a TheRace class which contains main(), setup() and draw(), a Race class which contains all the code for iterating through the race itself, a Racer class, and three different classes that extend Racer (different types of racers). I've imported the core.jar file, and there are no syntax errors (the code compiles), but when I run the code, I get this error:

Exception in thread "Animation Thread" java.lang.NullPointerException
at processing.core.PApplet.noStroke(PApplet.java:13845)
at Stepper.drawRacer(Stepper.java:15)
at Race.run(Race.java:29)
at TheRace.draw(TheRace.java:60)
at processing.core.PApplet.handleDraw(PApplet.java:2266)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
at processing.core.PApplet.run(PApplet.java:2140)
at java.lang.Thread.run(Thread.java:662)

Here is the Stepper class in which the error starts:

import processing.core.PApplet;

public class Stepper extends Racer {

    PApplet parent = new PApplet();

    Stepper(String name, int loc, int ypos, PApplet p) {
        super(1, name, loc, ypos,p);
    }

    public void move() {
        location += speed;
    }

    public void drawRacer(int size, int finish) {
        parent.noStroke();
        parent.fill(242,109,125);
        parent.rect((location*(1000-size*2))/finish, ypos, size, size);
    }
}

The constructor contains arguments for a name, location along the race, a lane location, and a PApplet argument that is required according to http://processing.org/learning/eclipse/. The error starts at parent.noStroke();, but isn't unique to noStroke(), as I have tried commenting it but the error just catches on the next line. I have a feeling that the error has something to do with how I'm using the PApplet, but I can't tell what.

Edit: so I've gotten input from someone I know, and apparently the problem is that I'm not using the correct PApplet parent. Here's the Racer class that Stepper extends:

import processing.core.*;

public class Racer{
  int location;
  int speed;
  String name;
  int ypos;
  PApplet parent;// = new PApplet();

  Racer (int initspeed, String nema, int loc, int yposi, PApplet p) {
    location = loc;
    speed = initspeed;
    name = nema;
    ypos = yposi;
    parent = p;
  }

  public PApplet getParent() {
    return parent;
  }
...

According to this person, it is wrong to declare a PApplet parent in both the Racer class and the Stepper class, but instead call the PApplet from the Racer class in the constructor of the Stepper class. However, what puzzles me is this: if parent is only ever declared in Racer, the Stepper class makes a reference to the Racer class, and I never tell the Racer class which PApplet to draw on (namely TheRace class which extends PApplet), then how will any of the Racer classes know which PApplet to draw on?

Thanks in advance.

1
Why do you want to move it to eclipse? i mean, couldn't you implement that class into processing?SuperKael
I have a class in eclipse that is supposed to interact with this processing class, a sort of setup menu that I made with JFrames and suchjaswon
oh, well, you can look at my answer, but i realy don't knowSuperKael

1 Answers

0
votes

The problem is that you create a new PApplet in Stepper (and maybe in whatever other subclasses you have). You only can have a single PApplet instance, which I assume is your TheRace class (which extends PApplet and implements setup and draw).

So, instead you need to pass through that instance to the classes which should have access to Papplet's methods. That is what you seem to do in Racer already. But simply remove the PApplet parent variable in the subclass (here: Stepper), and you are good.

And regarding your edit: You can access non-private variables from the superclass (here: Racer) in your subclasses (Stepper).