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

2017/2/9

NullPointer help

Samas Samas

2017/2/9

#
I am trying to change the image of a character (Sean) based on the first letter of an instance variable (String name) but I keep getting a nullPointer
public String setImages( String name )
        {        
        return name.substring(0,1);
        }
this is the method where I get a null pointer
private GreenfootImage right1 = new GreenfootImage( setImages(this.name) +"right1.png");
this is an example of how I am trying to use the method the idea is to change the color of the sheep based on the first letter. the images are named based on the color of the sheep EX. a red sheep for the first right animation would be rright1.png
Super_Hippo Super_Hippo

2017/2/9

#
If you get a Nullpointer Exception in line 3, it means that 'name' is null. Show how you declare (and maybe change) the String name.
Samas Samas

2017/2/9

#
        Sean player = new Sean("roy", false);
this is how I declare the String name.
Samas Samas

2017/2/9

#
I haven't created a way to change the name without hardcoding it yet
Samas Samas

2017/2/9

#
the first argument is the String name
Super_Hippo Super_Hippo

2017/2/9

#
This is not what I meant. The question is what you do with the "roy". It could for example look like this:
private String name;

public Sheep(String nameOfSheep)
{
    name = nameOfSheep;
}
Samas Samas

2017/2/9

#
public Sean( String name, boolean hasPowers )
        {
        this.name = name;
        this.hasPowers = hasPowers;
        
        }
Super_Hippo Super_Hippo

2017/2/9

#
Well, the problem is that this "private GreenfootImage right1 ..." line is executed before the constructor is executed. Try this:
private GreenfootImage right1;

public Sean(String name, boolean hasPowers)
{
    this.name;
    this.hasPowers = hasPowers;
    right1 = new GreenfootImage( setImages(this.name) +"right1.png");
}
Samas Samas

2017/2/9

#
Thanks it worked perfectly
Samas Samas

2017/2/9

#
actually there is one thing that I would like to change. The image is initially set to be the normal colored sheep and only updates the color after the player begins to move around. is there anyway to spawn in the correct colored sheep immediately?
Super_Hippo Super_Hippo

2017/2/9

#
Well, if you never want to change that image later, you can remove line 1 and change line 7 to:
setImage(new GreenfootImage( setImages(this.name) +"right1.png"));
Nosson1459 Nosson1459

2017/2/9

#
Samas wrote...
The image is initially set to be the normal colored sheep and only updates the color after the player begins to move around.
If the code is in the constructor then the sheep will get the right color when the 'Run' button is pressed, before the user starts to move around. If you want it to be the right color right when the scenario is started before run is pressed then you ca either add the sheep after run is pressed (not in world constructor) or you can put in your world constructor "Greenfoot.start()".
Samas Samas

2017/2/9

#
Okay that worked. Thanks for the help!
You need to login to post a reply.