有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

通过扩展类调用方法仅在方法被java重写时有效

我有4个类(核心类、游戏主类、精灵类、角色类)
-GameMain扩展核心(核心>;>;GameMain)
-字符扩展精灵(精灵>;>;字符)

如果我调用-b.get,我会得到一个NullPointerException。。。()-在角色中不重写它们的方法(它们最初在Spriter中,我通过角色从GameMain调用它们)

我在内核中创建角色对象,并将其放入ArrayList中

public abstract class Core {
  ArrayList<Character> mon = new ArrayList<Character>();
  void gameloop(){
     while(running){
       //some code
       while(mon.size() < 2){
          mon.add(new Character(blk_1,5,1));// Character(Spriter, Long, Long){}
       }
       draw(g);
     }
  }
}

然后在GameMain中,我调用了一个最初在Spriter中的方法,但我通过角色调用它

public class GameMain extends Core{
  public  void draw(Graphics2D g){
     for(Character b : mon){ //mon is the ArrayList<Character>

        if(test.getCoordY() <= b.getCoordY() - (b.getHeight()/2)){ //<<<< NullPointerException caused by these two Methods , test is a Spriter class

            g.drawImage(chChange(), Math.round(test.getX()), Math.round(test.getY()), test.getWidth(), (test.getHeight()), null); 
            g.drawImage(b.getImage(), Math.round(b.getX()), Math.round(b.getY()), null);

        }else {
            g.drawImage(b.getImage(), Math.round(b.getX()), Math.round(b.getY()), null);
            g.drawImage(chChange(), Math.round(test.getX()), Math.round(test.getY()), null);
        }
     } 
   }
}

这是Spriter的方法,除非我重写它,否则我将得到错误

public class Spriter {
   Spriter(Image i){
      this.i = i;
      scenes = new ArrayList();

   }

   Spriter(){
      scenes = new ArrayList();
      start();
   }

   public int getHeight(){
    return i.getHeight(null);
   }
   public float getCoordY(){
      float cy;
      cy = y + i.getHeight(null); //<<<< NullPointerException happens here, Image i;
      return cy;
   }

   public void setX(float x){
    this.x = x;
   }

   public void setY(float y){
    this.y = y;
   }
   // other similar Methods but no problem with them
   //-------------------------------------- Animation Part ----------------------------------------------

public synchronized void addScene(Image i,long t){
    total_t+=t;
    scenes.add(new oneScene(i, total_t));
}

//start animation
public synchronized void start(){
    mov_time = 0;
    scn_indx = 0;
}

    // get current scene
public synchronized Image getAnimeImage(){
    if(scenes.size()==0){
        return null;
    }else{
        return getScene(scn_indx).pic;
    }
}


//get the scene
private oneScene getScene(int x){
    return (oneScene)scenes.get(x);
}


 private class oneScene{
    Image pic;
    long endTime;

    public oneScene(Image pic, long endTime){
        this.pic = pic;
        this.endTime = endTime;

    }
}
}

如果我这样做,它会起作用:

public class Character extends Spriter{
  public Character(Spriter s, long health, long power) {
     this.s = s;
     this.health = health;
     this.power = power;

     s.setX(randomY());
     s.setY(randomY());
  }

  public float getCoordY(){
     return s.getCoordY();
  }

  public float getHeight(){
     return s.getgetHeight();
  }
  //some other methods for health and power 
}

但是如果我这样做,它能工作吗(它已经给出了错误,但如何避免它):

public class Character extends Spriter{
  public Character(Spriter s, long health, long power) {
      this.s = s;
      this.health = health;
      this.power = power;

      s.setX(randomY()); //setting x for the dynamiclly generated Character Object
      s.setY(randomY()); // same for y
  }

  //some methods for power and health
}

作为测试设置x,y(它是一个精灵,工作非常好)

public class GameMain extends Core{
  public void init(){
    test.setX(500); test.setY(488);
  }

  public  void draw(Graphics2D g){
    //the above code
  }
}

如果它们完全相同,我看不出有什么必要推翻它们


共 (2) 个答案

  1. # 1 楼答案

    我相信你的问题是你的类Character是一个Spriter并且有一个Spriter(而且可能不应该“有一个”Spriter,但我不知道,因为你没有发布所有的代码)。您从未初始化Charactersuper实例

    public class Character extends Spriter{
      public Character(Spriter s, long health, long power) {
        // super(health, power); // <  Speculation, no constructors shown in your code.
        this.s = s;
        this.health = health;
        this.power = power;
    
        super.setX(randomY()); //<  set the super value
        super.setY(randomY()); //<  again.
        // s.setX() // <  not sure, why do you have s?
      }
    
  2. # 2 楼答案

    问题是,当创建Character时,调用的是Spriter的无参数构造函数,而不是带有Image参数的构造函数。这意味着你的形象永远不会定型;这就是为什么会出现空指针异常

    您可能应该向Character的构造函数参数添加一个Image,并将其传递给超类构造函数,如下所示

    public Character(Spriter s, long health, long power, Image img) {
        super(img);
    
        // and so on.
    

    @ElliottFrisch-如果这是你试图用你的答案说的话,我很抱歉。我不想复制;我只是不确定你是不是这个意思。无论如何,这绝对是问题所在