I'm trying to make a simple game in Processing where you move an object around with changing sprites and a moving parallax background with three layers. I get this error when I run it from home.
java.lang.NullPointerException: Cannot read field "width" because "img" is null
at processing.core.PGraphics.image(PGraphics.java:3953)
at processing.core.PApplet.image(PApplet.java:12499)
at assignment_3$Background.parallax(assignment_3.java:79)
at assignment_3.draw(assignment_3.java:33)
at processing.core.PApplet.handleDraw(PApplet.java:2185)
at processing.awt.PSurfaceAWT$9.callDraw(PSurfaceAWT.java:1440)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:356)
It's probably something to do with calling the arrays but idk. I've tried to see if it's something with the setup inside of each class but I can't figure it out.
assignment_3
Player hero;
Background bg;
int animFrame = 0;
int cameraX = 0;
void setup() {
size(400, 180);
frameRate(120);
hero = new Player(10, 10, 0, 0);
bg = new Background();
}
void draw() {
animFrame = (frameCount / 3) % 6;
bg.parallax(cameraX);
if (keyPressed) {
hero.acc();
}
else {
hero.stop();
}
if(hero.x > 300) {
cameraX += 1;
}
hero.step();
hero.animate(animFrame);
}
Background
class Background {
PImage[] background = new PImage[3];
int cameraX;
int world2Screen(int x, int y, int z) {
return(x - cameraX)/z;
}
void setup() {
background[0] = loadImage("https://ecs.wgtn.ac.nz/foswiki/pub/Courses/CGRA151_2022T2/Assignment3/background_layer_1_sm.png");
imageDouble(background[0]);
background[1] = loadImage("https://ecs.wgtn.ac.nz/foswiki/pub/Courses/CGRA151_2022T2/Assignment3/background_layer_2_sm.png");
imageDouble(background[1]);
background[2] = loadImage("https://ecs.wgtn.ac.nz/foswiki/pub/Courses/CGRA151_2022T2/Assignment3/background_layer_3_sm.png");
imageDouble(background[2]);
}
void imageScale(PImage toScale, float scale){
toScale.resize(int(toScale.width*scale),int(toScale.height*scale));
}
void imageDouble(PImage toScale){
// toScale.resize(int(toScale.width*2),int(toScale.height*2));
// but better to reuse
imageScale(toScale,2.0);
}
void parallax(int cam) {
if(cam == 0) {
image(background[0], world2Screen(0, 0, 3), 0);
image(background[0], world2Screen(background[0].width*((cam/background[0].width) + 3),0, 3), 0);
image(background[1], world2Screen(0, 0, 2), 0);
image(background[1], world2Screen(background[1].width*((cam/background[1].width) + 3),0, 3), 0);
image(background[2], world2Screen(0, 0, 1), 0);
image(background[2], world2Screen(background[2].width*((cam/background[2].width) + 3),0, 3), 0);
}
}
}
Player
class Player {
float x, y, runSpeed, jumpSpeed;
int animFrame = 0;
int charW = 56;
String direction;
boolean jump;
PImage idlesheetl = loadImage("https://ecs.wgtn.ac.nz/foswiki/pub/Courses/CGRA151_2022T2/Assignment3/idle_l.png");
PImage idlesheetr = loadImage("https://ecs.wgtn.ac.nz/foswiki/pub/Courses/CGRA151_2022T2/Assignment3/idle_r.png");
PImage[] idlel = new PImage[6];
PImage[] idler = new PImage[6];
PImage runsheetl = loadImage("https://ecs.wgtn.ac.nz/foswiki/pub/Courses/CGRA151_2022T2/Assignment3/run_l.png");
PImage runsheetr = loadImage("https://ecs.wgtn.ac.nz/foswiki/pub/Courses/CGRA151_2022T2/Assignment3/run_r.png");
PImage[] runl = new PImage[8];
PImage[] runr = new PImage[8];
PImage jumpsheetl = loadImage("https://ecs.wgtn.ac.nz/foswiki/pub/Courses/CGRA151_2022T2/Assignment3/jump_l.png");
PImage jumpsheetr = loadImage("https://ecs.wgtn.ac.nz/foswiki/pub/Courses/CGRA151_2022T2/Assignment3/jump_r.png");
PImage[] jumpl = new PImage[10];
PImage[] jumpr = new PImage[10];
Player(float xPos,float yPos,float rSpeed,float jSpeed) {
x = xPos;
y = yPos;
runSpeed = rSpeed;
jumpSpeed = jSpeed;
direction = "right";
jump = false;
}
void setup(){
for(int i = 0; i < idlel.length; i++) {
idlel[i] = idlesheetl.get(i*charW, 0, charW, charW);
idler[i] = idlesheetr.get(i*charW, 0, charW, charW);
}
for(int i = 0; i < runl.length; i++) {
runl[i] = runsheetl.get(i*charW, 0, charW, charW);
runr[i] = runsheetr.get(i*charW, 0, charW, charW);
}
for(int i = 0; i < jumpl.length; i++) {
jumpl[i] = jumpsheetl.get(i*charW, 0, charW, charW);
jumpr[i] = jumpsheetr.get(i*charW, 0, charW, charW);
}
}
void animate(int animFrame) {
if(jump) {
if(direction == "right") {
image(jumpr[animFrame], x, (height - y) - charW);
}
else {
image(jumpl[animFrame], x, (height - y) - charW);
}
}
if(!jump) {
if(runSpeed == 0 && jumpSpeed == 0) {
if(direction == "right") {
image(idler[animFrame], x, (height - y) - charW);
}
else {
image(idlel[animFrame], x, (height - y) - charW);
}
}
}
if(runSpeed > 0) {
if(direction == "right") {
image(runr[animFrame], x, (height - y) - charW);
}
else {
image(runl[animFrame], x, (height - y) - charW);
}
}
}
void step() {
if(y < 10) {
y = 10;
}
if(y > 150) {
y = 150;
}
if(y > 0 && x < width - 60) {
x += runSpeed;
}
else if (x <= 0) {
x = 10;
}
else {
x = 315;
}
if(y < height) {
y += jumpSpeed;
}
}
void acc() {
}
void stop() {
}
}