How to pass a PApplet in composition? (I am using Intelij as an ide)

I am making this in Intelij;

First class; import processing.core.PApplet;

    public class Test1 {
        static PApplet parent;
        public Test1(PApplet p) {
            parent = p;}

        Test2 test2 = new Test2(parent);

        public void dosomething(){

            test2.doThing();
        }
    }

Second class; import processing.core.PApplet; public class Test2 {

    static PApplet parent;
    public Test2(PApplet p) {
        parent = p;}

    public void doThing(){  parent.ellipse( 100,100,50,50);  }
}

Then in the main class public class ExampleApplet extends PApplet {

        int countSVGsUsed = 0;
        String [] SvgsUsed = new String[1000];


        public static void main(String args[]) {
            PApplet.main("ExampleApplet");
        }
    public void draw() {
      Test1 test1 = new Test1(this);
            test1.dosomething();
    }
    }

this gives me the following errors

smooth() can only be used inside settings() Exception in thread "Animation Thread" java.lang.NullPointerException at Test2.doThing(Test2.java:12) at Test1.dosomething(Test1.java:15) at ExampleApplet.draw(ExampleApplet.java:101) at processing.core.PApplet.handleDraw(PApplet.java:2401) at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1499) at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:312)

I get a NullPointerException because PApplet does not get passed(?) is there a workaround or a simple solution for this?

Thx in advance

Answers

  • The first message about smooth is just a warning. Make your own settings() and setup() methods in your main class if you want to get rid of it.

    The null pointer error is because in the Test1 class, you declare and initialise a Test2 object using a reference to its parent PApplet variable at object creation, before the constructor, which is always null. You should only initialise the Test2 object in or after the constructor:

    public Test1(PApplet p) {
            parent = p;
            test2 = new Test2(parent);
    }
    
    Test2 test2; 
    
  • It worked!! Thank you very much, now I can finish my project.

    Problem was that it was getting too big, the processing IDE only feels comfortable if the number of classes used are small, and it gets cluttered very quickly, too much code, too much classes. That is why I migrated my project to Intelij. But I was a bit wary from doing this in the first place because of this PApplet issue... now its solved

Sign In or Register to comment.