semiserious
semiserious

Reputation: 610

Android Game Dev - NullPointerException after declaring new object (constructor not doing what I want it to do)

I'm currently learning game programming using a framework called libgdx. It allows users to make programs that can easily run on both a Java virtual machine on the desktop and an Android mobile phone. I am trying to make a basic program that will display a character on screen and allow me to move it back and forth with the keyboard. I have created 3 classes - one called Assets, which loads files into the memory and converts them into Textures and Animations, one called InputHandler, which responds to key input by changing animations, direction of movement etc., and one called FightView, which is essentially where all the rendering gets done. The InputHandler is called from the FightView class with the typical command input = new InputHandler(anim, frame);. The constructor for InputHandler looks like this:

public InputHandler(Animation animation, TextureRegion region){

    Assets.load();  //loads textures from files into memory and creates animations from them
    animation = Assets.playStance;  //set initial animation to be used
    region = animation.getKeyFrame(0, true);    //make TextureRegion equal to first frame in animation

}

Now, what I want to happen is simply that the Animation and TextureRegion passed into the constructor are set to the values quoted above, so that, for example, if I was to mention region in the FightView class after input had been declared, it would be set as animation.getKeyFrame(0, true);. From my limited experience of Java this is what I believe should happen, but I get a NullPointerException at compile time pointing to the second line in the code below:

    input = new InputHandler(anim, frame);
    x = centreX - (frame.getRegionWidth() / 2);

Clearly, frame is still null, even after being passed through the constructor, which (as can be see in the third line in the constructor) should assign to it a value. Note that before being passed into the constructor they both are actually null - I am trying to create a class that will give the fields data without needing to do so in the FightView class.

Any help appreciated. This could be the wrong way to go about this - what I am trying to do is create an object that, upon initialisation, will load the Animation and TextureRegions with data.

Upvotes: 0

Views: 582

Answers (1)

Walt Armour
Walt Armour

Reputation: 344

The problem you are having is due to a misunderstanding of a basic Java concept. Java passes parameters by value, not reference. What you have written will never work as you expect.

First, I recommend you perhaps tackle a simpler problem or take a side trip to reinforce some of the basic Java concepts and object concepts.

Second, here's a brief answer to achieve what you desire.

Add members to the InputHandler class to hold an Animation and a TextureRegion. In the constructor (which would take no parameters) you would assign the values like you have done except you would assign them to the member variables.

class InputHandler {

    public Animation animation;
    public TextureRegion region;

    public InputHandler() {

        Assets.load();  //loads textures from files into memory and creates animations from them
        animation = Assets.playStance;  //set initial animation to be used
        region = animation.getKeyFrame(0, true);    //make TextureRegion equal to first frame in animation

    }
}

After constructing the InputHandler you could reference it's members (frame in this case) and see your correct values.

input = new InputHandler();
x = centreX - (input.frame.getRegionWidth() / 2);

Upvotes: 1

Related Questions