Null pointer exception
in
Core Library Questions
•
7 months ago
I get a null pointer exception error message and all i am trying to do is apply text to the programe with an int for the player score... However when i call upon the function of the ScoreUp class in draw, i get the error. Seems silly for such a simple thing!!!
Any hep appreciatted, ill just pop the code up....
//Main class
Star [] star = new Star [50];//Declare as a collection/array of stars and the size - 50.
Enemy [] enemy = new Enemy[3];//Declare classes
Planet [] planet = new Planet[6];
Enemy [] enemy = new Enemy[3];//Declare classes
Planet [] planet = new Planet[6];
ArrayList bulletCollection;
Score score;
User user;
User user;
import ddf.minim.*;
Minim minim;
AudioPlayer player;
AudioSample crash;
AudioPlayer player;
AudioSample crash;
void setup() {
size(1000, 700, P3D);
smooth();
size(1000, 700, P3D);
smooth();
minim = new Minim(this);
player = minim.loadFile("theme.mp3");
//player.play();
crash = minim.loadSample("crash.mp3");
player = minim.loadFile("theme.mp3");
//player.play();
crash = minim.loadSample("crash.mp3");
bulletCollection = new ArrayList();
for (int i = 0; i < 6; i ++) {
planet [i] = new Planet(random(0, width), random(0, height), 15, 15);
}
planet [i] = new Planet(random(0, width), random(0, height), 15, 15);
}
for (int i = 0; i < enemy.length; i ++) {
enemy [i] = new Enemy(1050, random(100, 600), 50, 50);//initialize parameters in here
}
enemy [i] = new Enemy(1050, random(100, 600), 50, 50);//initialize parameters in here
}
PImage simg = loadImage("ship.jpg");
user = new User(75, 350, simg);
user = new User(75, 350, simg);
for (int i = 0; i < 50; i ++) {
star [i] = new Star(random (0, width), random(0, height), 5, 5);
}
}
star [i] = new Star(random (0, width), random(0, height), 5, 5);
}
}
void draw() {
background(0);
noStroke();
score.displayScore();
noStroke();
score.displayScore();
for (int i = 0; i < bulletCollection.size() ; i ++) {
Bullets bullets = (Bullets) bulletCollection.get(i);
bullets.run();
}
Bullets bullets = (Bullets) bulletCollection.get(i);
bullets.run();
}
for (int i = 0; i < 50; i ++) {
star[i].run();//call upon all elements of the array. If i was a 0 itwould call one star
star[i].move();
if (star[i].x < 0) {
star [i] = new Star( width, random(0, height), 5, 5);
}
}
star[i].run();//call upon all elements of the array. If i was a 0 itwould call one star
star[i].move();
if (star[i].x < 0) {
star [i] = new Star( width, random(0, height), 5, 5);
}
}
for (int i = 0; i < enemy.length; i ++) {
enemy[i].run();
}
enemy[i].run();
}
for (int i = 0; i < 6; i ++) {
planet[i].display();
planet[i].move();
if (planet[i].x < 0) {
planet [i] = new Planet(random(1000, 1200), random(0, height), 15, 15);
}
}
planet[i].display();
planet[i].move();
if (planet[i].x < 0) {
planet [i] = new Planet(random(1000, 1200), random(0, height), 15, 15);
}
}
user.run();
}
}
void keyPressed() {
//println(user.playerSpeedX);
user.move();
}
//println(user.playerSpeedX);
user.move();
}
void mousePressed() {
Bullets bullets = new Bullets(user.imgX + 172, user.imgY + 42, 5, 2);
bulletCollection.add(bullets);
}
bulletCollection.add(bullets);
}
void stop()
{
// always close Minim audio classes when you are done with them
player.close();
crash.close();
minim.stop();
{
// always close Minim audio classes when you are done with them
player.close();
crash.close();
minim.stop();
super.stop();
}
}
//Bullet Class
class Bullets {
float x, y, w, h;
float bulletSpeed = 8;
boolean hit = false;
boolean active = true;
int playerScore = 0;
float bulletSpeed = 8;
boolean hit = false;
boolean active = true;
int playerScore = 0;
Bullets(float posX, float posY, float Width, float Height) {
x = posX;
y = posY;
w = Width;
h = Height;
}
y = posY;
w = Width;
h = Height;
}
void run() {
display();
moveBullet();
collision();
}
display();
moveBullet();
collision();
}
void display() {
fill (255, 255, 44);
rect(x, y, w, h);
}
fill (255, 255, 44);
rect(x, y, w, h);
}
void moveBullet() {
x += bulletSpeed;
if (x >= width) {
active = false;
}
}
x += bulletSpeed;
if (x >= width) {
active = false;
}
}
void collision() {
for (int i = 0; i < bulletCollection.size();i++) {
Bullets bullets = (Bullets) bulletCollection.get(i);
for (int j = 0; j < enemy.length ; j ++) {
if (x+w >= enemy[j].x && x <= enemy[j].x + enemy[j].w
&& y+h >= enemy[j].y && y <= enemy[j].y + enemy[j].h && active != false) {
enemy[j].x = width;
hit = true;
bulletCollection. remove(i);
println(playerScore);
}
else {
hit = false;
}
}
}
}
}
for (int i = 0; i < bulletCollection.size();i++) {
Bullets bullets = (Bullets) bulletCollection.get(i);
for (int j = 0; j < enemy.length ; j ++) {
if (x+w >= enemy[j].x && x <= enemy[j].x + enemy[j].w
&& y+h >= enemy[j].y && y <= enemy[j].y + enemy[j].h && active != false) {
enemy[j].x = width;
hit = true;
bulletCollection. remove(i);
println(playerScore);
}
else {
hit = false;
}
}
}
}
}
//Enemy Class
class Enemy {
float x, y, w, h;
float Xspeed = -7;
float Yspeed = -5;
float YspeedC = 2;
boolean collide = false;
float collideFall = 8;
float fillCol = 0;
float Xspeed = -7;
float Yspeed = -5;
float YspeedC = 2;
boolean collide = false;
float collideFall = 8;
float fillCol = 0;
Enemy (float posX, float posY, float Width, float Height) {
x = posX;
y = posY;
w = Width;
h = Height;
}
x = posX;
y = posY;
w = Width;
h = Height;
}
void run() {
show();
move();
changeover();
collide();
boom();
reset();
}
show();
move();
changeover();
collide();
boom();
reset();
}
void show() {
fill(255, 255, fillCol);
rect(x, y, w, h);
}
fill(255, 255, fillCol);
rect(x, y, w, h);
}
void move() {
x += Xspeed;
y += Yspeed;
}
x += Xspeed;
y += Yspeed;
}
void changeover() {
if (y <= 100|| y >= 600) {
Yspeed = Yspeed *-1;
}
}
if (y <= 100|| y >= 600) {
Yspeed = Yspeed *-1;
}
}
void collide() {
if (x >= user.imgX && x <= user.imgX + 172
&& y >= user.imgY && y <= user.imgY + 90) {
Xspeed = 0;
Yspeed = 0;
collide = true;
}
}
if (x >= user.imgX && x <= user.imgX + 172
&& y >= user.imgY && y <= user.imgY + 90) {
Xspeed = 0;
Yspeed = 0;
collide = true;
}
}
void boom() {
if ((collide)) {
user.imgY += collideFall;
//crash.trigger();
}
else {
collide = false;
}
}
if ((collide)) {
user.imgY += collideFall;
//crash.trigger();
}
else {
collide = false;
}
}
void reset() {
if (x <= 0) {
x = width;
y = random(0,700);
}
}
}
if (x <= 0) {
x = width;
y = random(0,700);
}
}
}
//planet class
class Planet{
float x,y,w,h;
float planetSpeed = 5;
Planet(float PosX, float PosY, float Width, float Height){
x = PosX;
y = PosY;
w = Width;
h = Height;
}
void display(){
fill(random(0,255),random(0,255), random(0,255));
ellipse(x,y,w,h);
noStroke();
}
void move(){
x -= planetSpeed;
}
}
//Score class
float x,y,w,h;
float planetSpeed = 5;
Planet(float PosX, float PosY, float Width, float Height){
x = PosX;
y = PosY;
w = Width;
h = Height;
}
void display(){
fill(random(0,255),random(0,255), random(0,255));
ellipse(x,y,w,h);
noStroke();
}
void move(){
x -= planetSpeed;
}
}
//Score class
class Score {
int playerScore = 0;
int playerScore = 0;
void displayScore() {
fill(255);
textSize(30);
text (playerScore, 50, 50);
}
}
fill(255);
textSize(30);
text (playerScore, 50, 50);
}
}
//Star Class
class Star {
float x, y, w, h;
float starSpeed = 7;
Star( float posx, float posy, float posw, float posh ) {
x = posx;
y = posy;
w = posw;
h = posh;
}
void run(){
display();
}
void display(){
fill(255);
ellipse(x,y,w,h);
strokeWeight(2);
}
void move(){
x -= starSpeed;
}
}
float x, y, w, h;
float starSpeed = 7;
Star( float posx, float posy, float posw, float posh ) {
x = posx;
y = posy;
w = posw;
h = posh;
}
void run(){
display();
}
void display(){
fill(255);
ellipse(x,y,w,h);
strokeWeight(2);
}
void move(){
x -= starSpeed;
}
}
//User Class
class User {
float x, y, w, h;
float playerSpeedY = 15;
float playerSpeedX = 15;
PImage img;
float imgX = 75;
float imgY = 350;
float playerSpeedY = 15;
float playerSpeedX = 15;
PImage img;
float imgX = 75;
float imgY = 350;
User (float posX, float posY, PImage img) {
this.x = posX;
this.y = posY;
this.img = img;
}
this.x = posX;
this.y = posY;
this.img = img;
}
void run() {
display();
bulletTracker();
}
display();
bulletTracker();
}
void display() {
image(img, imgX, imgY);
}
}
void bulletTracker() {
fill(255, 255, 0);
ellipse(imgX + 172, imgY + 45, 10, 10);
}
fill(255, 255, 0);
ellipse(imgX + 172, imgY + 45, 10, 10);
}
void move() {
if (keyCode == UP) {
imgY -= playerSpeedY;
}
else if (keyCode == DOWN) {
imgY += playerSpeedY;
}
else if (keyCode == LEFT) {
imgX -= playerSpeedX;
}
else if (keyCode == RIGHT) {
imgX += playerSpeedX;
}
}
}
if (keyCode == UP) {
imgY -= playerSpeedY;
}
else if (keyCode == DOWN) {
imgY += playerSpeedY;
}
else if (keyCode == LEFT) {
imgX -= playerSpeedX;
}
else if (keyCode == RIGHT) {
imgX += playerSpeedX;
}
}
}
Many thanks!
1