0
votes
/**
 * 
 */
package be.pandapp.ehb;

This is the first class.

import processing.core.PApplet;
import processing.core.PShape;

    /**
     * @author Pindamannetje
     *
     */
    public class SlashTag extends DisplayObject {


        PShape headAndFoot;
        PShape middle;
        PShape text;
        float scale;


        public SlashTag(float x, float y, float z, int fillColor, float scale, PShape headAndFoot, PShape middle, PShape text) {
            super(x, y, z);
            this.headAndFoot = headAndFoot;
            this.middle = middle;
            this.text = text;
            this.scale = scale;

        }


        public float getScale() {
            return scale;
        }


        public void setScale(float scale) {
            this.scale = scale;
        }

        public PShape getMiddle() {
            return middle;
        }



        public void setMiddle(PShape middle) {
            this.middle = middle;
        }


        public PShape getHeadAndFoot() {
            return headAndFoot;
        }


        public void setHeadAndFoot(PShape headAndFoot) {
            this.headAndFoot = headAndFoot;
        }



        public PShape getText() {
            return text;
        }



        public void setText(PShape text) {
            this.text = text;
        }


        @Override
        public void draw(PApplet app) {
            /*app.fill(fillColor);
            app.stroke(strokeColor);
            app.strokeWeight(strokeWeight);*/
            app.pushMatrix();
            app.translate(x, y);
            app.scale(scale);
            app.shape(headAndFoot);
            app.shape(middle);
            app.shape(text);
            app.popMatrix();
        }



    }

This is the class where I try to create a PApplet in but gives an error.

package be.pandapp.ehb;

import processing.core.*;

public class Slashing extends SlashTag {

    boolean mouseAfterClick;
    boolean outOfPlace;
    int lastMouseX;
    PApplet app;

    public Slashing(float x, float y, float z, int fillColor, float scale,
            PShape headAndFoot, PShape middle, PShape text, PApplet app) {
        super(x, y, z, fillColor, scale, headAndFoot, middle, text);
        this.app = app;
        // TODO Auto-generated constructor stub

    }

    int mouseX2 = (int) (app.mouseX * 0.245);
    int lastMouseX2 = (int) (lastMouseX * 0.245);

    public void draw() {
        if (app.mouseX >= 410 && app.mousePressed) {

            outOfPlace = true;

            app.shape(middle, app.mouseX, 492 - (mouseX2));
        }

        else if (lastMouseX < 420) {
            outOfPlace = false;
            mouseAfterClick = false;
        }

        if (outOfPlace && mouseAfterClick && app.mousePressed == false) {

            app.shape(middle, lastMouseX, 492 - (lastMouseX2));
            lastMouseX -= 15;
        }

        else if (outOfPlace == false) {

        }

    }

    public void mouseReleased() {

        mouseAfterClick = true;
        lastMouseX = app.mouseX;
    }

}

The error gives is a nullpointer.

Sorry forgot about the main class.

here it is:

import be.pandapp.ehb.Slashing;
import be.pandapp.ehb.Slashing;

import processing.core.PApplet;
import processing.core.PShape;

/**
 * 
 */

/**
 * @author AdrienSchautteet
 * 
*/

public class MainSlashTag extends PApplet {

    /**
     * @param args
     */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    PApplet.main(new String[] { "PandApp" });
}

private int aantal = 15;
Slashing Slashing[] = new Slashing[aantal];
private PShape headAndFoot;
private PShape middle;
private PShape text;

// ---

boolean mouseAfterClick;
boolean outOfPlace;
int lastMouseX;

public void setup() {
    size(1280, 800);
    background(0);
    smooth();
    headAndFoot = loadShape("../images/SlashTagProcessingHeadFoot.svg");
    middle = loadShape("../images/SlashTagProcessingMiddle.svg");
    text = loadShape("../images/SlashTagProcessingText.svg");

    text.disableStyle();

    outOfPlace = false;
}

public void draw() {

    for (int i = 0; i < this.aantal; i++) {


        if (i == 0) {
            Slashing[i] = new Slashing(random(0, 100),
                    random(0, 100), 0, 255, 1, headAndFoot, middle,
                    text,this);

            Slashing[i].draw(this);
        }

        if (i < 5 && i > 0) {
            Slashing[i] = new Slashing(random(Slashing[i - 1].getX() + 150,
                    Slashing[i - 1].getX() + 275), random(0, 100),
                    0, 255, 1, headAndFoot, middle, text,this);
            Slashing[i].draw(this);



        }

        if (i == 5) {
            Slashing[i] = new Slashing(random(0, 100),
                    random(250, 350), 0, 255, 1, headAndFoot, middle,
                    text,this);

            Slashing[i].draw(this);
        }

        if (i < 10 && i > 5) {


            Slashing[i] = new Slashing(random(Slashing[i - 1].getX() + 150,
                    Slashing[i - 1].getX() + 275), random(250, 350),
                    0, 255, 1, headAndFoot, middle, text,this);
            Slashing[i].draw(this);

            System.out.println(Slashing[i].getX());

        }

        if (i == 10) {
            Slashing[i] = new Slashing(random(0, 100),
                    random(500, 600), 0, 255, 1, headAndFoot, middle,
                    text,this);

            Slashing[i].draw(this);
        }


        if (i <= 15 && i > 10) {
            Slashing[i] = new Slashing(random(Slashing[i - 1].getX() + 150,
                    Slashing[i - 1].getX() + 275), random(500, 600),
                    0, 255, 1, headAndFoot, middle, text,this);
            Slashing[i].draw(this);

            System.out.println(Slashing[i].getX());

        }

        stop();
    }
}

}

This is the error:

Exception in thread "Animation Thread" java.lang.NullPointerException

     at be.pandapp.ehb.Slashing.<init>(Slashing.java:20)
     at MainSlashTag.draw(MainSlashTag.java:58)
     at processing.core.PApplet.handleDraw(PApplet.java:2305)
     at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
     at processing.core.PApplet.run(PApplet.java:2176)
     at java.lang.Thread.run(Thread.java:722)
1
What is DisplayObject?Petros Koutsolampros

1 Answers

1
votes

The problem is that (in the Slashing class) you are trying to give a value to the field mouseX2 when the class is initialized, no matter if you put it after the constructor. Any value assignment in a class that is not in a constructor or method (or an inner class) will be executed when a reference of this class is created, BEFORE the constructor. Since you call to fill the variable "this.app" with an incoming "app" value inside the constructor, before it "app" has not been defined, is thus null. Then you try to access it to get mouseX and the jvm throws you a nullpointer...

Thus, the lines:

public Slashing(float x, float y, float z, int fillColor, float scale,
        PShape headAndFoot, PShape middle, PShape text, PApplet app) {
    super(x, y, z, fillColor, scale, headAndFoot, middle, text);
    this.app = app;
    // TODO Auto-generated constructor stub

}
int mouseX2 = (int) (app.mouseX * 0.245);
int lastMouseX2 = (int) (lastMouseX * 0.245);

Should become:

int mouseX2,lastMouseX2;

public Slashing(float x, float y, float z, int fillColor, float scale,
        PShape headAndFoot, PShape middle, PShape text, PApplet app) {
    super(x, y, z, fillColor, scale, headAndFoot, middle, text);
    this.app = app;
    // TODO Auto-generated constructor stub
    mouseX2 = (int) (app.mouseX * 0.245);
    lastMouseX2 = (int) (lastMouseX * 0.245);
}