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

2015/1/10

getworld() returns null

M138A M138A

2015/1/10

#
In the constructor Containergame2 I want to get all the objects of the class MainContainer. But getWorld keeps returning null. the constructor:
1
2
3
4
public Containergame2(){
        setContainer(getRandomContainerType());
       
    }
The method getRandomContainerType:
1
2
3
4
5
6
7
8
public int getRandomContainerType(){  
        
       MiniGame2 world = (MiniGame2) getWorld();
       System.out.println(world); //returns null
       System.out.println(world.getObjects(MainContainer.class)); // returns null
    
       return Greenfoot.getRandomNumber(4);
   }
So what I want is that in the constructor it gives me a list with all the objects of the class MainContainer. What am I doing wrong?
Super_Hippo Super_Hippo

2015/1/10

#
The constructor is called before the object is added to the world, so 'getWorld' won't work there. What you could do is, use the 'addedToWorld' method which is automatically called once right after the object was added to the world or you pass the world as a parameter to the constructor and use this reference.
davmac davmac

2015/1/10

#
The constructor is called before the object is added to the world, so 'getWorld' won't work there
Minor correction: The method works perfectly - it does exactly what it is supposed to do: "Return the world that this actor lives in." - the null return means that there is no such world, which is correct. What Super_Hippo meant was that the method doesn't do what you think it should.
Super_Hippo Super_Hippo

2015/1/10

#
Right, it returns 'null' and you can't call methods on 'null'. ;)
M138A M138A

2015/1/10

#
The world is called MiniGame2. My ContainerGame2 constructor now looks like this:
1
2
3
4
public Containergame2(MiniGame2 x){
       setContainer(getRandomContainerType(x));
       //imageSet = false;
   }
And getRandomContainerType looks like this:
1
2
3
4
5
6
public int getRandomContainerType(MiniGame2 y){      
       //MiniGame2 world = (MiniGame2) getWorld();
       System.out.println(y.getListnul());
    
       return Greenfoot.getRandomNumber(4);
   }
But I also have a subclass Wagon with a empty constructor. It gives me this error : "constructor Containergame2 in class Containergame2 cannot be applied to given types. required: MiniGame2; found:no arguments " My wagon class looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Wagon here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Wagon extends Containergame2
{   
     
    private int keepMoving ;
    private int stoppingMarker;
    private int speed = 1;
    public Wagon(){//this is where the error points to
     
    }
     
    /**
     * Act - do whatever the Wagon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here. 
        Train x = (Train) getWorld().getObjects(Train.class).get(0);
        if(x.speed == 1 ){
            moveWagon();
        }
        if(isAtEdge())
        {
            getWorld().removeObject(this);    
 
        }
    }   
    /**
     * Checks if the wagon has arrived at it's stopping point
     */
    private boolean wagonHasToStop()
    {
        if(stoppingMarker == 850 && getY() == 550)
        {
            return true;
        }
        else if(stoppingMarker == 750 && getY() == 620)
        {
            return true;
        }
        else if (stoppingMarker == 650 && getY() == 690)
        {
            return true;
        }
        else{
            return false;
        }
    }
    /**
     * Moves the wagon at a certain speed
     */
    private void moveWagon()
    {
        setLocation(getX(), getY() - speed);
    }
 
  
    
}
Why does it give this error?
danpost danpost

2015/1/10

#
You made Wagon a subclass of Containergame2. Since there is no constructor in the Containergame2 class that has no arguments (it only has the one which has one MiniGame2 argument), Wagon cannot construct properly without the argument. If you are creating the Wagon objects from the world class (of MiniGame2 type), you can use 'new Wagon(this)' to create a wagon.
M138A M138A

2015/1/11

#
danpost wrote...
You made Wagon a subclass of Containergame2. Since there is no constructor in the Containergame2 class that has no arguments (it only has the one which has one MiniGame2 argument), Wagon cannot construct properly without the argument. If you are creating the Wagon objects from the world class (of MiniGame2 type), you can use 'new Wagon(this)' to create a wagon.
I don't make wagons in the world. That happens in a class called Train, with the following method:
1
2
3
4
5
6
private void createNewWagon(int spawnPoint)
    {
 
        Wagon w = new Wagon((MiniGame2) getWorld());
        getWorld().addObject(w,40,900);
    }
The int spawnpoint is needed but I still need to implement that. So first it was Wagon w = new wagon(); but the change to Wagon w = new Wagon((MiniGame2) getWorld()) still gives the same error. How can I make it work?
danpost danpost

2015/1/11

#
Remove lines 14 through 16 of the Wagon class (the constructor).
M138A M138A

2015/1/11

#
danpost wrote...
Remove lines 14 through 16 of the Wagon class (the constructor).
When I remove these lines the same error shows at the following line:
1
public class Wagon extends Containergame2
What did I do wrong? Or is there an other way to get a list of all the instances of the MainContainer class in the constructor for Containergame2?
danpost danpost

2015/1/11

#
Now, add this constructor where you had the old one:
1
2
3
4
public Wagon(Minigame2 x)
{
    super(x);
}
M138A M138A

2015/1/11

#
danpost wrote...
Now, add this constructor where you had the old one:
1
2
3
4
public Wagon(Minigame2 x)
{
    super(x);
}
Thanks danpost that works!
You need to login to post a reply.