I am using two sprites as parameters for a method but I am getting a null pointer exception error. Here is my code. The errors are in the checkCollision method. I was trying to declare the parameters Sprite s1 and Sprite s2 but they were still not recognized. I am not sure what I did wrong because when I declared other Sprites as parameters there was no null pointer exception. I would really appreciate your help. Thank you in advance.
Sprite p;
Sprite e;
Sprite f;
Sprite q;
Sprite l;
Sprite m;
Sprite n;
PImage red_brick;
PImage other_block;
final static float MOVE_SPEED = 5;
final static float SPRITE_SCALE = 50.0/128;
final static float SPRITE_SIZE = 50.0;
final static float GRAVITY = 0.6;
ArrayList <Sprite> platforms;
void setup(){
size(1000,600);
createPlatforms("data/map.csv");
red_brick = loadImage("block.png");
other_block = loadImage("diamond.png");
platforms = new ArrayList <Sprite>();
platforms.add(e);
platforms.add(f);
platforms.add(q);
platforms.add(l);
platforms.add(m);
platforms.add(n);
imageMode(CENTER);
p = new Sprite("data/sprite.png", 0.5,67,450);
e = new Sprite("data/block.png", 1.0, 67, 600);
f = new Sprite("data/block.png", 1.0, 195, 600);
q = new Sprite("data/block.png", 1.0, 323, 600);
l = new Sprite("data/block.png", 1.0, 451, 600);
m = new Sprite("data/diamond.png", 1.0, 451, 472);
n = new Sprite("data/bigblock.png", 1.0, 907,600);
resolvePlatformCollisions(p, platforms);
}
void draw(){
background(255);
p.display();
e.display();
f.display();
q.display();
l.display();
m.display();
n.display();
p.update();
}
boolean checkCollision(Sprite s1, Sprite s2){
boolean noXOverlap = s1.getRight() <= s2.getLeft()|| s1.getLeft() >= s2.getRight(); Error
boolean noYOverlap = s1.getBottom() <= s2.getTop() || s1.getTop() >= s2.getBottom(); Error
if (noXOverlap || noYOverlap){
return false;
}
else{
return true;
}
}
public void resolvePlatformCollisions(Sprite s, ArrayList <Sprite> walls){
s.change_y += GRAVITY;
s.center_y += s.change_y;
ArrayList <Sprite > col_list = checkCollisionList(s, walls);
if(col_list.size() > 0){
Sprite collided = col_list.get(0);
if(s.change_y > 0){
s.setBottom(collided.getTop());
}
else if(s.change_y >0){
s.setTop(collided.getBottom());
}
s.change_y = 0;
}
s.center_x += s.change_x;
col_list = checkCollisionList(s, walls);
if(col_list.size() > 0){
Sprite collided = col_list.get(0);
if(s.change_x > 0){
s.setRight(collided.getLeft());
}
else if(s.change_x >0){
s.setLeft(collided.getRight());
}
s.change_x = 0;
}
}
public ArrayList <Sprite> checkCollisionList(Sprite s, ArrayList <Sprite> list){
ArrayList <Sprite> collision_list = new ArrayList<Sprite>();
for(Sprite p : list){
if (checkCollision(s,p))
collision_list.add(p);}
return collision_list;
}
void keyPressed(){
if(keyCode == RIGHT){
p.change_x = 5;
}
else if(keyCode == LEFT){
p.change_x = -5;
}
else if (keyCode == UP){
p.change_y = -5;
}
else if (keyCode == DOWN){
p.change_y = 5;}
}
void keyReleased(){
if(keyCode == RIGHT){
p.change_x = 0;
}
else if(keyCode == LEFT){
p.change_x = 0;
}
else if(keyCode == UP){
p.change_y =0;
}
else if (keyCode == DOWN){
p.change_y = 0;}
}
void createPlatforms(String filename){
String[] lines = loadStrings(filename);
for(int row = 0; row <lines.length; row++){
String [] values = split(lines [row], ",");
for(int col = 0; col< values.length; col++){
if(values[col].equals("1")){
Sprite s = new Sprite (red_brick, SPRITE_SCALE);
s.center_x = SPRITE_SIZE/2 + col * SPRITE_SIZE;
s.center_y = SPRITE_SIZE/2 + row * SPRITE_SIZE;
}
if (values[col].equals("2")){
Sprite s = new Sprite (other_block, SPRITE_SCALE);
s.center_x = SPRITE_SIZE/2 + col * SPRITE_SIZE;
s.center_y = SPRITE_SIZE/2 + row * SPRITE_SIZE;
}
}
}
}
e
, l
, f
, ... are "Null Pointers". No object is assigned to this references. You need to create the Sprite
objects, before you append them to the ArrayList
:
void setup() {
// [...]
platforms = new ArrayList <Sprite>();
e = new Sprite(... your parameters for sprite e ...);
platforms.add(e);
f = new Sprite(... your parameters for sprite f ...);
platforms.add(f);
// [...]