I want to get an attribute from an specific class, the problem is that this class where i'm trying to get the attribute from is an abstract superclass and the instance that i'm trying to call is from a child class of this superclass, I can not call the method from the child class as the instance I need to call may differ depending of the user choice in execution time.
public abstract class player implements stuff {
int attribute a;
public Player(int location, Map map){
this.attribute a = attribute a;
}
public getAttribute(){
return a;
}
}
public abstract class Knight extends player {
public Knight(int location, Map map){
super(a);
this.a = fixedValue;
}
}
public class GameState implementes state { //this is where I instance the class
Map map;
Player player;
public gameState(){
map = new Map(rute);
//Here there may be several options but just a single instance will be created
if(condition){
player = new Knight(location, map);
}else{
player = new Mague(location, map);
}... //don't mind the if/else clauses
}
public void draw(Graphics g){
map.draw(g, player.getLocation());
player.draw(g);
}
}
public class PauseMenu{
public PauseMenu(Player player){
int height = value;
Color color = value;
.... more irrelevant stuff
}
public void drawInfo(Graphics g, Player player){
drawString(g, positionValues, player.getAttribute); // this is where my code fails
}
}
I'm getting a null pointer exception, my guess is that I'm trying to access a value in the parent class that only exists on the child class (or at least is initialized in child class), , maybe because I'm using a 'player' instance from 'Player' class and not from the child class, so I was trying to figure out how to call the method from the class 'GameState' which is where the "true" instance is created but I don't have any idea how to do it, I would appreciate any help from you guys, and please don't mind my rookie code.
Answering someone's question: Knight class has his own constructor (sorry I mistyped) but shares some information with player Class as well as some other classes such as Mage, I'm not instancing Player class, i'm instancing Knight or Mage, depending on user's choice. DrawInfo method is called from another class called PauseState, in that class I had to create an item called Player player in order to be able to pass the information to the drawInfo method, I assume that's the reason of the exception in my programm, I want to know how can I use the player object that's in GameState into my PauseState class so I don't have to create that player object with not initialized arguments.