We are about to switch to a new forum software. Until then we have removed the registration on this forum.
in my tick method processing throws a null pointer exception when i try to use an image. I know all image names are correct and that the images are in the correct folder
///*global variables*///
//arraylist for obsticals
ArrayList <os> a = new ArrayList <os>();
//hard codeing color vavibles for good practice
final color RED = color(255, 0, 0);
final color BLUE = color(0, 0, 255);
final color BLACK = color(0, 0, 0);
//array for different x positons used by the obticals
float[] xvalues = {
1000/5, 1000/2, 1000*5/6
};
//difficulty to increase rate of change for speed
int difficulty = 10;
//failsafe for speed change when it hits a certain score
boolean f = true;
//speed at whitch obsticals travel
int speedg = 5;
//timeing vabiable used to spawn obsticals at a certain intervals
int time = 0;
//varaiable declared so a random positon can be choisen
int choice;
//
int rimage;
//orginal respawn time
int respawntime = 0;
//telles if car is alive
boolean caralive = true;
//score variable
int score = 0;
car mycar;
//images for sprites
PImage bigtruck;
PImage tanker;
PImage firetruck;
PImage blacklimo;
PImage pokelimo;
PImage velocity;
//spritearray, used in os class
PImage[] spritearray = {
bigtruck, tanker, firetruck, blacklimo, pokelimo, velocity
};
PImage sprcar;
///*Setup*///
void setup() {
size(1000, 1000);
fill(255, 0, 0);
imageMode(CENTER);
mycar = new car(RED, 333);
a.add(new os(1, xvalues[choice], 0, speedg));
sprcar=loadImage("Car 57tr.jpg");
bigtruck =loadImage("adgtruck.gif");
tanker=loadImage("CAR1_44.bmp");
firetruck=loadImage("CAR1_42.bmp");
blacklimo=loadImage("blacklimo.bmp");
pokelimo=loadImage("CAR2_86.bmp");
velocity=loadImage("Velocityp.gif");
}
///*Draw*///
void draw() {
background(0);
for (int i= 0; i <a.size(); i++)
a.get(i).tick();
if (caralive)
mycar.tick2();
time++;
if (time % 360 == 0) {
choice = int (random(3));
//picks random number for ues in spritearray
rimage = int (round(random(4)));
a.add(new os(rimage, xvalues[choice], 0, speedg));
}
if ( respawntime > 0)
respawntime--;
if ( respawntime == 0)
caralive = true;
if (score % difficulty == 0 && score !=0 && f == true) {
speedg+=1;
f = false;
println(f);
println(speedg);
}
if (score % difficulty != 0 && f == false)
f = true;
strokeWeight(10);
stroke(255, 255, 0);
line(width/3, 0, width/3, 1000);
line(width*2/3, 0, width*2/3, 1000);
textSize(32);
text(score, width/2, 32);
}
///*Classes and methods*////
//obsticals the car has to doge
class os {
int mynumber;
float xpos;
float ypos;
float speed;
public os (int sprnumber, float nx, float xy, float sp) {
mynumber = sprnumber;
xpos = nx;
ypos = xy;
speed = sp;
}
//displays obsticals
void tick() {
strokeWeight(2);
//ellipse(xpos,ypos,100,150);
image(spritearray[mynumber], xpos, ypos, 100, 150);
step();
if ( ypos+116 > 850 && xpos == mycar.x ) {
killselfandcar();
respawntime = 120;
}
}
//moves obsticlas
void step() {
ypos = ypos + speed;
if ( ypos > height ) {
destroy();
}
}
public void destroy() {
for ( int i = 0; i < a.size(); i++) {
if (a.get( i ) == this ) {
a.remove( i );
score++;
}
}
}
public void killselfandcar() {
caralive = false;
destroy();
score-=2;
}
}
//car that you use
class car {
color c;
float x;
public car (color cl, float xpos) {
c = cl;
xpos = x;
}
//method to have car move into one of 3 lines
void step() {
if (mouseX < width/3) {
x = width/5;
}
if (mouseX > width/3 && mouseX < width*2/3) {
x = width/2;
}
if (mouseX > width*2/3) {
x = width*5/6;
}
}
//display method
void tick2() {
fill(c);
stroke(0);
strokeWeight(2);
image(sprcar, x, 900, 120, 160);
step();
}
}
Answers
The
spritearray
array is built at declaration time, before setup() is run. It uses the values of the variables you used, values they have at build time. So, it is just full of null values, since the variables have not been initialized yet.Solution: just declare
PImage[] spritearray;
ie. without initializing it.After the loadImage calls, you can init the array:
The
new PImage[]
part is necessary when you init an array outside of a declaration.