0

I'm trying to recreate tetris using processing for a school project. But I keep getting nullPointerExceptions when trying to call the fill function

this is the class:

package nl.HAN.game.eindOpdracht;

import processing.core.PApplet;

public class Block {
    private PApplet app;
    private int xPos;
    private int yPos;
    private int width;
    private int height;
    private boolean exploded;

    public Block(PApplet app, int xPos, int yPos) {
        //standaard
        this.app = app;
        this.xPos = xPos;
        this.yPos = yPos;

        this.width = (app.width / 3) / 10;
        this.height = app.height / 20;
        this.exploded = false;
    }

    public boolean isExploded() {
        return exploded;
    }

    public void explode(){
        this.exploded = true;
    }

    public void drop(){
        this.yPos++;
    }

    public void move(int direction){
        if(direction == 0){
            this.xPos++;
        } else {
            this.xPos--;
        }
    }

    public void draw(){
        if(!exploded){
            app.fill(200, 100, 100);
        }
    }
}

this is the main class:

public static void main(String[] args) {

    PApplet app = new PApplet();

    //app.print("test");

    Block block = new Block(app, 1, 1);

    //block.draw();
    app.fill(255);

    String[] processingArgs = {"nl.han.ica.oopd.waterworld.WaterWorld"};
    Main mySketch = new Main();

    //PApplet.runSketch(processingArgs, mySketch);
}

and this is the error:

Exception in thread "main" java.lang.NullPointerException
    at processing.core.PApplet.fill(PApplet.java:14739)
    at nl.HAN.game.eindOpdracht.Main.main(Main.java:18)
arrayZero
  • 352
  • 1
  • 3
  • 8
  • Perhaps [this](https://stackoverflow.com/q/52594045/7210739) is a better duplicate. You're not using Processing correctly and shouldn't create a PApplet the way you're doing so, ` PApplet app = new PApplet();` – DontKnowMuchBut Getting Better Jun 07 '20 at 14:41
  • For decent information on Processing, search on [anything posted by Kevin Workman](https://stackoverflow.com/search?q=user%3A873165+%5Bprocessing%5D) as he knows more about this subject than anyone I've seen on this site, and even the site agrees: [ranking link](https://stackoverflow.com/tags/processing/topusers) – DontKnowMuchBut Getting Better Jun 07 '20 at 14:43
  • thx I'll try those – arrayZero Jun 08 '20 at 16:02
  • I used the wrong PApplet, I should have used ` Block block = new Block(this, 1, 1) ` instead of creating a new PApplet – arrayZero Jun 10 '20 at 14:39

0 Answers0