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

2017/10/29

Why doesn`t it work? Reference to world

wueschn wueschn

2017/10/29

#
Hello everyone I have a problem with this piece of code. My animation should change sometimes a text. I made in my constructor class Rechtecke a reference to world with also a field world. But I always get a Nullpointer Exception, but I can' t understand it. Thanks a lot for your help Mario
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import greenfoot.*;
public class Rechtecke extends Actor
{
    private int r,g,b;
    private World world;//Reference zu World;
    public Rechtecke()
    {
        GreenfootImage image = new GreenfootImage(450,300);
        image.setColor(new Color(255,0,0,255));
        image.fillRect(0, 0, 450, 300);
        this.setImage(image);
        //causes Nullpointer Exception
        world = this.getWorld();
        world.showText("Hello World", 50, 50);
    }
danpost danpost

2017/10/29

#
The 'getWorld' method will return a null value anytime the Actor object is not in any world. The constructor block (lines 6 through 15) is executed when a Rechtecke object is being created and before it can ever be placed into a world. Make use of the Actor class method 'addedToWorld' to load the 'world' field and show the text.
wueschn wueschn

2017/10/29

#
Hello danpost Thanks for your quick answer. Unfortunately I don`t understand your solution. I post again my class Rechtecke AND the MyWorld Class. Could you please implement your solution to these classes?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import greenfoot.*;
public class Rechtecke extends Actor
{
    private int r,g,b;
    private World world;//Reference zu World;
    public Rechtecke()
    {
        GreenfootImage image = new GreenfootImage(450,300);
        image.setColor(new Color(255,0,0,255));
        image.fillRect(0, 0, 450, 300);
        this.setImage(image);
        //causes Nullpointer Exception
        world = this.getWorld();
        world.showText("Hello World", 50, 50);
    }
1
2
3
4
5
6
7
8
9
10
11
12
public class MyWorld extends World
{
 
    public MyWorld()
    {   
         
        super(600, 400, 1);
        Rechtecke r = new Rechtecke();
        this.addObject(r, 300, 220);
    }
 
}
Thanks a lot. Mario
danpost danpost

2017/10/29

#
Start the Rechtecke class like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import greenfoot.*;
 
public class Rechtecke extends Actor
{
    private int r, g, b;
    private World world;
 
    public Rechtecke()
    {
        GreenfootImage image = new GreenfootImage(450, 300);
        image.setColor(Color.RED);
        image.fill();
        this.setImage(image);
    {
 
    protected void addedToWorld(World world)
    {
        this.world = world;
        world.showText("Hello World", 50, 50);
    }
The addedToWorld method is called automatically when an Actor object is placed into a world.
wueschn wueschn

2017/10/29

#
@danpost Perfect, it works perfect. Thanks for your help, I learned a lot! One last question. protected void addedToWorld(World world) ... the "world" parameter is an automatically generated instance of the abstract class World? Mario
danpost danpost

2017/10/29

#
wueschn wrote...
protected void addedToWorld(World world) ... the "world" parameter is an automatically generated instance of the abstract class World?
I would not say automatic. It is a reference to a previously created world that the actor was added into. I am not sure why it is passed as a parameter for the method as calling 'getWorld' returns that world when the method is executed (probably just a matter of taste -- having it referenced there already).
wueschn wueschn

2017/10/29

#
OK, thanks a lot!!
You need to login to post a reply.