Multiple Classes Not Working

edited January 2017 in Questions about Code

How would I use rect() from a class other than my Main? I just get the error:

java.lang.NullPointerException
at processing.core.PApplet.rect(PApplet.java:11559)
at Player.frame(Player.java:21)
at Main.draw(Main.java:40)
at processing.core.PApplet.handleDraw(PApplet.java:2418)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1540)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

My main class is

import processing.core.PApplet;

public class Main extends PApplet {
    public static void main(String args[]) {
        PApplet.main("Main");
    }
    public static int sizeX;
    public static int sizeY;
    public void settings() {
        size(1440,810);
        sizeX = width;
        sizeY = height;
    }

    Player player = new Player((int) (width/2), (int) (height/2));
    byte[][] world = new byte[100][50];

    public void setup() {
        rectMode(CENTER);
        for(int x = 0; x<world.length; x++) {
            for(int y = 0; y<world[0].length; y++) {
                if(y == world[0].length-1 || Math.random()<=0.1) {
                    world[x][y] = 1;
                }
            }
        }
    }

    public void draw() {
        for(int x = player.getX()-120; x<player.getX()+width; x+= 40) {
            for(int y = player.getY()-120; y<player.getY()+height; y+= 40) {
                if((int)(x/40)<=world.length && (int) (y/40)<=world[0].length  && (int) (x/40)>=0 && (int) (y/40) >=0) {
                    //System.out.println(x/20 + " " + y/20);
                    if(world[x/40][y/40] > 0) {
                        rect(x,y,40,40);
                    }
                }
            }
        }
        player.frame(world);
    }
}

and my player class is

import processing.core.PApplet;

public class Player {
    int x = 0;
    int y = 0;

    public Player(int stX, int stY) {
        x = stX;
        y = stY;
   }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    boolean up = false;
    boolean down = false;
    boolean left = false;
    boolean right = false;

    public void frame(byte[][] world) {
        rect(Main.sizeX/2,Main.sizeY/2,80,40);
    }

    public void keyPressed() {
    }
}
Tagged:

Answers

  • edited February 2019 Answer ✓

    If you had seen other Processing's 3rd-party libraries, for most of them, you may had noticed we pass this as their 1st argument when instantiating them.

    Each "ignited" PApplet got a PGraphics canvas.
    And in order for other top classes to access it, they need to know the reference to the target PApplet.

    Change your class' constructor to include an extra parameter. And save it inside a new field: *-:)

    import processing.core.PApplet;
    
    public class Player {
      protected final PApplet p;
      public int x, y;
    
      public Player(final PApplet pa, final int px, final int py) {
        p = pa;
    
        x = px;
        y = py;
    
        setStyle();
      }
    
      public Player setStyle() {
        p.colorMode(PApplet.RGB);
        p.blendMode(PApplet.REPLACE);
        p.rectMode(PApplet.CORNER);
    
        p.fill(0xffFF00FF);
        p.stroke(0);
        p.strokeWeight(2.5f);
    
        return this;
      }
    
      public Player display() {
        p.rect(x, y, p.width>>1, p.height>>1);
        return this;
      }
    }
    
Sign In or Register to comment.