This site requires JavaScript, please enable it in your browser!
Greenfoot back
ddvink
ddvink wrote ...

2013/10/23

NullPointerException

ddvink ddvink

2013/10/23

#
In the constructor I can reach the experience of the Rogue-object, but why not in the keyController?
private GreenfootImage experienceImage;
    private String experienceText = "Temporary text";
    private int experience = 0;
    private Rogue rogue;

    // Constructor
    public Experience(){
    }

    // Constructor met Variable for Rogue
    public Experience(Rogue rogue){
        this.experience = rogue.getRogueExperience();

        setExperience(rogue.getRogueExperience());
        setExperienceText();
    }

    // Temp to higher the rogue xp
    public int keyController(Rogue rogue){
        int temp;
        if(Greenfoot.isKeyDown("q")){
            System.out.println("Temporary xp added to rogue");
            temp = rogue.getRogueExperience();            
            System.out.println(temp);            
        }
        return experience;
    } 
danpost danpost

2013/10/23

#
You are not doing anything to actually 'higher the rogue xp' in the method. You are declaring the local field 'temp', sending some text and the assigned value of that local field to the terminal window, and returning a value from an instance field not previously worked on in the method.
Kartoffelbrot Kartoffelbrot

2013/10/23

#
I guess this isn't the whole code. If it is, please post the rest,
ddvink ddvink

2013/10/23

#
Well, the issue is that in the constructor the this.experience = rogue.getRogueExperience(); works well, but in the method keyController the temp = rogue.getRogueExperience(); doesn't work (for now on, the rogueExperience is hardcoded set on 125 in the Rogue class). It's giving the nullPointer message.... why is that? yours, Dennis
Kartoffelbrot Kartoffelbrot

2013/10/23

#
Maybe you don't donate the right rogue as a parameter? Maybe you wanted to use your private variable rogue, but it it has no value/object.
ddvink ddvink

2013/10/23

#
I get it now!!! Thanx for waking me up! Yours, Dennis
danpost danpost

2013/10/23

#
Things would be a lot easier to work with if you just coded the Experience class like a basic Text class and controlled the text in the actor class whose values are to be displayed. Because the image is a mystery to me, I could not exactly what needs to be done, but it would be something like this:
import greenfoot.*;
import java.awt.Color;

public class Experience extends Actor
{
    public Experience(String text)
    {
        setText(text);
    }

    public void setText(String text)
    {
        // code from your 'setExperienceText' method
    }
}
Then in your Rogue class:
// add instance field
int experience = 100;
Experience xpText = new Experience("");
// in constructor
xpText.setText("XP: "+experience); // whatever text
// anytime you change the experience value
xpText.setText("XP: "+experience): // whatever
// may need this method
public int getExperience()
{
    return experience;
}
// and possibly this one (but I do not see why, right now)
public Experiencee getXPText()
{
    return xpText;
}
You need to login to post a reply.