空指针异常错误作为方法参数

Posted

技术标签:

【中文标题】空指针异常错误作为方法参数【英文标题】:Null pointer exception error as method parameter 【发布时间】:2021-08-22 23:42:14 【问题描述】:

我正在使用两个精灵作为方法的参数,但我得到一个空指针异常错误。 这是我的代码。 错误在 checkCollision 方法中。 我试图声明参数 Sprite s1 和 Sprite s2 但它们仍然无法识别。我不确定我做错了什么,因为当我将其他 Sprite 声明为参数时,没有空指针异常。我将衷心感谢您的帮助。提前谢谢你。

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;
     
    
  


【问题讨论】:

错误出现在 checkCollision 方法上 空指针异常 【参考方案1】:

e, l, f, ... 是“空指针”。没有对象分配给此引用。您需要先创建Sprite 对象,然后再将它们附加到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);

    // [...]

【讨论】:

以上是关于空指针异常错误作为方法参数的主要内容,如果未能解决你的问题,请参考以下文章

规避空指针异常

获取空指针异常错误空值被传递

捕获空指针异常是代码异味吗?

使用TestNG运行代码时出现空指针异常

Java空指针异常解决方法

c++异步回调函数引用传递空指针异常