I've been having a problem with a null object reference in one of my constructors that calls on a get method from another class. If anyone has any idea how it could be happening it would be a great help.
Heres my Logcat:
And the code in the Player class :
And here is the class with the getPlayerImage method:
(Sorry about the formatting of this post, Im new to using forums and didn't know how to sort it properly}
Heres my Logcat:
Code:
2019-03-13 18:39:14.553 11395-11426/uk.ac.qub.eeecs.gage E/Gage Error:: Attempt to invoke virtual method 'android.graphics.Bitmap uk.ac.qub.eeecs.game.cardDemo.PlayerInfo.getPlayerImage()' on a null object reference
2019-03-13 18:39:14.555 11395-11426/uk.ac.qub.eeecs.gage E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: uk.ac.qub.eeecs.gage, PID: 11395
java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap uk.ac.qub.eeecs.game.cardDemo.PlayerInfo.getPlayerImage()' on a null object reference
at uk.ac.qub.eeecs.game.cardDemo.Player.<init>(Player.java:22)
at uk.ac.qub.eeecs.game.cardDemo.PlayerObject.<init>(PlayerObject.java:73)
at uk.ac.qub.eeecs.game.cardDemo.CardDemoScreen.<init>(CardDemoScreen.java:261)
at uk.ac.qub.eeecs.game.cardDemo.CardDemoMenuScreen.updateButtons(CardDemoMenuScreen.java:170)
at uk.ac.qub.eeecs.game.cardDemo.CardDemoMenuScreen.update(CardDemoMenuScreen.java:143)
at uk.ac.qub.eeecs.gage.Game.doUpdate(Game.java:363)
at uk.ac.qub.eeecs.gage.Game.access$200(Game.java:27)
at uk.ac.qub.eeecs.gage.Game$GameLoop.run(Game.java:557)
at java.lang.Thread.run(Thread.java:764)
And the code in the Player class :
Code:
private int playerHealth;
private int playerCoins;
private int cardCost;
private Bitmap mPlayerImage;
private Bitmap mPlayer;
public Player(float startX, float startY, float width, float height, GameScreen gameScreen, PlayerInfo playerInfo) {
22 super(startX, startY, width, height, playerInfo.getPlayerImage(), gameScreen);
playerHealth = playerInfo.getHealth();
playerCoins = playerInfo.getPlayerCoins();
mPlayerImage = playerInfo.getPlayerImage();
mPlayer = mPlayerImage;
}
public void draw(ElapsedTime elapsedTime, IGraphics2D graphics2D,
LayerViewport layerViewport, ScreenViewport screenViewport) {
// Draw the player
mBitmap = mPlayer;
super.draw(elapsedTime, graphics2D, layerViewport, screenViewport);
}
public float getX(){
return position.x;
}
public float getY(){
return position.y;
}
public Bitmap getBitmap() {
return mPlayer;
}
And here is the class with the getPlayerImage method:
Code:
public class PlayerInfo {
private Bitmap mPlayerImage;
private int mHealth;
private int mPlayerCoins;
/**
* Create a CardInfo object
* [USER=315340]@param[/USER] playerImage
* [USER=315340]@param[/USER] health
* [USER=315340]@param[/USER] playerCoins
*/
public PlayerInfo(Bitmap playerImage, int health, int playerCoins) {
mPlayerImage = playerImage;
mHealth = health;
mPlayerCoins = playerCoins;
}
public Bitmap getPlayerImage() {
return mPlayerImage;
}
(Sorry about the formatting of this post, Im new to using forums and didn't know how to sort it properly}
Last edited by a moderator: