Pergunta

i have 4 classes (Core, GameMain, Spriter, Character)
- GameMain extends core (core >> GameMain)
- Character extends Spriter (Spriter >> Character)

I get a NullPointerException if i call the - b.get...() - Methods without overriding them in Character (they are originally inside Spriter, i call them from GameMain through Character).

i make objects of Character inside Core and put it in an 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);
     }
  }
}

then inside GameMain i call a Method that is originally inside Spriter but i call it through Character.

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);
        }
     } 
   }
}

Here is the Spriter's Method, unless i override it i will get the error.

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;

    }
}
}

it would work if i do this :

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 
}

but can it work if i do this (it is already givinng the error but how to avoid it) :

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
}

as setting x,y for test (its a sprite and working very good)

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

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

i dont see the point of overriding them if they will be exactly the same.

Foi útil?

Solução

Your problem is that when you create a Character, you're calling the no-arg constructor of Spriter, instead of the one with an Image argument. That means that your image is never getting set; which is why you have the null pointer exception.

You should probably add an Image to the constructor arguments of Character, and pass this along to the superclass constructor, like this.

public Character(Spriter s, long health, long power, Image img) {
    super(img);

    // and so on.

@ElliottFrisch - I apologise if that is what you were trying to say with your answer. I don't mean to duplicate; I just wasn't quite sure whether that is what you meant. In any case, this is definitely the problem.

Outras dicas

I believe your problem is that your class Character is-a Spriter and has-a Spriter (and probably shouldn't "have-a" Spriter but I can't tell because you didn't post all of your code). You never initialized the super instance of your Character.

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?
  }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top