NullPointerException
in
Programming Questions
•
1 year ago
Hi I'm new to programming and a little help with this processing error would be much appreciated.
I am at the start of trying to code a Lemmings like game called Phlegmmings for uni but i get a NullpointerException when running it.
Phlegm[] phlegm; //an array of phlegm
Timer timer; //one timer object
PFont f;
void setup() {
size(800, 800);
smooth();
frameRate(10);
phlegm = new Phlegm[10]; //creates 10 phlegm
timer = new Timer(300); //create a timer that goes off every 2 seconds
timer.start(); //start the timer
f = createFont("Arial", 12, true); // A font to write text on the screen
// for (int i = 0; i < phlegm.length; i++) {
// phlegm[i] = new Phlegm[10]; //initialising each phlegm
// }
}
void draw() {
background(0);
// If the game is over
// if (gameOver) {
// textFont(f, 48);
// textAlign(CENTER);
// fill(0);
// text("GAME OVER", width/2, height/2);
// }
// else {
for (int i = 0; i < phlegm.length; i++) {
phlegm[i].move(); //this is where he error appears and if i comment it out it happens on the line below too...
phlegm[i].display(); //if they are both commented out the programme runs ok but without displaying or moving the array of phlegm
}
// }
}
class Phlegm {
float c; //color
float s = 10; //size of phlegm
float xpos, ypos; //variables for location of phlegm
float xspeed; // x speed of phlegm
float yspeed; // y speed of phlegm
float gravity;
// //new variable to keep track of whether phlegm is still alive
// boolean dead = false;
Phlegm () {
c = color(20, 200, 20); //all phlegms are the same color
xpos = width/2; //starting location x
ypos = height/2; //starting location y
xspeed = 1; //all plegms same speed
// gravity = 0.98;
yspeed = 1;
}
// void setLocation(float tempX, float tempY){
// x = tempX; //temporary location x
// y = tempY; //temporary location y
// }
//movement of phlegm
void move() {
//drop from start
ypos += yspeed;
if (ypos > height-10) {
ypos = height-10;
}
xpos = xpos + xspeed;
if ((xpos >= width-10) || (xpos < 10)) {
xspeed = xspeed * -1;
}
}
void display () {
stroke(0);
fill(20, 200, 20);
ellipse(xpos, ypos, s*2, s*2);
}
}
class Timer {
int savedTime; // When Timer started
int totalTime; // How long Timer should last
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void setTime(int t) {
totalTime = t;
}
// Starting the timer
void start() {
// When the timer starts it stores the current time in milliseconds.
savedTime = millis();
}
// The function isFinished() returns true if 5,000 ms have passed.
// The work of the timer is farmed out to this method.
boolean isFinished() {
// Check how much time has passed
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
}
else {
return false;
}
}
}
Any help will kindly be appreciated. I'm also looking for tips on programming a lemmings style game if anybody knows a good source of info that would also be helpful.
Thank you in advance.
Joel
1