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

2019/1/11

Moving Scenery NullPointerException

The_Beast0007 The_Beast0007

2019/1/11

#
This is the same project I have posted about recently: Click Here This time when I try to run it the greenfoot terminal pops up and throws an exception: My World code is here:
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
import greenfoot.*;
 
public class OverWorld extends World
{
    public OverWorld()
    {   
        super(1200, 800, 1, false); //Creates a new world measuring 1200 x 800 cells with a cell size of 1x1 pixels and no boundaries.
        prepare();
    }
    private Jumper jumper;
    public Jumper getDidBumpHead() {
        return jumper;
    }
    public Jumper getIsOnSolidGround() {
        return jumper;
    }
    public Jumper getCanMoveLeft() {
        return jumper;
    }
    public Jumper getCanMoveRight() {
        return jumper;
    }
    private void prepare()
    {
        setPaintOrder(Jumper.class, Platform.class);
        Jumper jumper = new Jumper();
        addObject(jumper, 600, 400);
        Scenery scenery = new Scenery(3600, 2400);
        addObject(scenery, 600, 400);
        Platform platform = new Platform(50, 1200);
        addObject(platform, 1800, 400);
    }
}
My 'Scenery' code is here:
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
import greenfoot.*;
 
public class Scenery extends Actor
{
    private int step = 3;
    private boolean DBH = false;
    private boolean IOSG = false;
    private boolean CML = false;
    private boolean CMR = false;
    public void boolSet() {
        if(((OverWorld) getWorld()).getDidBumpHead().didBumpHead()) DBH = true;
        else if(!((OverWorld) getWorld()).getDidBumpHead().didBumpHead()) DBH = false;
        if(((OverWorld) getWorld()).getIsOnSolidGround().isOnSolidGround()) IOSG = true;
        else if(!((OverWorld) getWorld()).getIsOnSolidGround().isOnSolidGround()) IOSG = false;
        if(((OverWorld) getWorld()).getCanMoveLeft().canMoveLeft()) CML = true;
        else if(!((OverWorld) getWorld()).getCanMoveLeft().canMoveLeft()) CML = false;
        if(((OverWorld) getWorld()).getCanMoveRight().canMoveRight()) CMR = true;
        else if(!((OverWorld) getWorld()).getCanMoveRight().canMoveRight()) CMR = false;
         
    }
    public void Move() {
        int y = getY();
        int x = getX();
        if(Greenfoot.isKeyDown("w") && !DBH) y += step; 
        if(Greenfoot.isKeyDown("s") && !IOSG) y -= step;
        if(Greenfoot.isKeyDown("a") && CML) x += step; 
        if(Greenfoot.isKeyDown("d") && CMR) x -= step;
        setLocation(x, y);
         
    }
    public Scenery(int width, int height) {
        GreenfootImage image = getImage();
        image.scale(width, height);
        setImage(image);
    }
    public void act()
    {
        boolSet();
        Move();
    }   
}
My 'Platform' code is here:
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
import greenfoot.*;
 
public class Platform extends Actor
{
    private int step = 3;
    private boolean DBH = false;
    private boolean IOSG = false;
    private boolean CML = false;
    private boolean CMR = false;
     
    public void boolSet() {
        if(((OverWorld) getWorld()).getDidBumpHead().didBumpHead()) DBH = true;
        else if(!((OverWorld) getWorld()).getDidBumpHead().didBumpHead()) DBH = false;
        if(((OverWorld) getWorld()).getIsOnSolidGround().isOnSolidGround()) IOSG = true;
        else if(!((OverWorld) getWorld()).getIsOnSolidGround().isOnSolidGround()) IOSG = false;
        if(((OverWorld) getWorld()).getCanMoveLeft().canMoveLeft()) CML = true;
        else if(!((OverWorld) getWorld()).getCanMoveLeft().canMoveLeft()) CML = false;
        if(((OverWorld) getWorld()).getCanMoveRight().canMoveRight()) CMR = true;
        else if(!((OverWorld) getWorld()).getCanMoveRight().canMoveRight()) CMR = false;
    }
    public Platform() {
        this(100, 25);
    }
     
    public void Move()
    {
        int y = getY();
        int x = getX();
        if(Greenfoot.isKeyDown("w") && !DBH) y += step; 
        if(Greenfoot.isKeyDown("s") && !IOSG) y -= step;
        if(Greenfoot.isKeyDown("a") && CML) x += step; 
        if(Greenfoot.isKeyDown("d") && CMR) x -= step;
        setLocation(x, y);
         
    }
    public Platform(int width, int height) {
        GreenfootImage image = getImage();
        image.scale(width, height);
        setImage(image);
    }
    public void act()
    {
        boolSet();
        Move();
    }   
}
And finally my 'Jumper' code is here: (I commented out my old fall, jump and move methods as this game doesn't require them)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import greenfoot.*;
 
public class Jumper extends Actor
{
    private final int GRAVITY = 1;
    private final int STEP = 5;
    private int velocity;
    private int aniTimeD = 0;
    private int aniTimeA = 0;
    private int aniTimeW = 0;
    private int aniTimeS = 0;
    private int imageHeight = 100;
    private int imageWidth = 75;
     
      
     public Jumper() {
        this(75, 100);
        velocity = 0;
    }
     
    public Jumper(int width, int height) {
        GreenfootImage image = getImage();
        image.scale(width, height);
        setImage(image);
     }
     
    public void act()
    {
        /**fall();
        if (Greenfoot.isKeyDown("space") && isOnSolidGround()) jump();
        move();**/
        if(aniTimeD == 19) aniTimeD = 0;
        if(aniTimeA == 19) aniTimeA = 0;
        if(aniTimeW == 19) aniTimeW = 0;
        if(aniTimeS == 19) aniTimeS = 0;
        GreenfootImage image = getImage();
        if (Greenfoot.isKeyDown("w") && !Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("s") && !Greenfoot.isKeyDown("d") && !didBumpHead())
        {
            aniTimeW = aniTimeW + 1;
            if (aniTimeW == 1) setImage(new GreenfootImage("GuyBack.png"));
            if (aniTimeW == 7) setImage(new GreenfootImage("GuyBack2.png"));
            if (aniTimeW == 13) setImage(new GreenfootImage("GuyBack3.png"));
        }
        if (!Greenfoot.isKeyDown("w") && !Greenfoot.isKeyDown("a") && Greenfoot.isKeyDown("s") && !Greenfoot.isKeyDown("d") && !isOnSolidGround())
        {
            aniTimeD = aniTimeD + 1;
            if (aniTimeD == 1) setImage(new GreenfootImage("GuyFront.png"));
            if (aniTimeD == 7) setImage(new GreenfootImage("GuyFront2.png"));
            if (aniTimeD == 13) setImage(new GreenfootImage("GuyFront3.png"));
        }   
         
        if (!Greenfoot.isKeyDown("w") && !Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("s") && Greenfoot.isKeyDown("d") && canMoveRight())
        {
            aniTimeD = aniTimeD + 1;
            if (aniTimeD == 1) setImage(new GreenfootImage("GuyRight.png"));
            if (aniTimeD == 7) setImage(new GreenfootImage("GuyRight2.png"));
            if (aniTimeD == 13) setImage(new GreenfootImage("GuyRight3.png"));
        }
        if (!Greenfoot.isKeyDown("w") && Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("s") && !Greenfoot.isKeyDown("d") && canMoveLeft())
        {
            aniTimeA = aniTimeA + 1;
            if (aniTimeA == 1) setImage(new GreenfootImage("GuyLeft.png"));
            if (aniTimeA == 7) setImage(new GreenfootImage("GuyLeft2.png"));
            if (aniTimeA == 13) setImage(new GreenfootImage("GuyLeft3.png"));
             
        }
    }   
     
    /**public void fall() {
        setLocation(getX(), getY() + velocity);
        if (isOnSolidGround()) {
            velocity = 0;
             
            while (isOnSolidGround()) {
                setLocation(getX(), getY() - 1);
            }
            setLocation(getX(), getY() + 1);
        }
        else if (velocity < 0 && didBumpHead()) {
            velocity = 0;
             
            while (didBumpHead()) {
                setLocation(getX(), getY() + 1);
            }
        }
        else velocity += GRAVITY;
    }**/
     
    public void jump() {
        velocity = -20;
    }
        
    /**public void move() {
        int y = getY();
        int x = getX();
        if(Greenfoot.isKeyDown("a") && canMoveLeft()) x -= STEP; 
        if(Greenfoot.isKeyDown("d") && canMoveRight()) x += STEP;
        setLocation(x, y);
    }**/
     
    public boolean isOnSolidGround() {
        boolean isOnGround = false;
         
        if (getY() > getWorld().getHeight() - 50) isOnGround = true;
         
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneObjectAtOffset(imageWidth / -2, imageHeight / 2, Platform.class) != null ||
            getOneObjectAtOffset(imageWidth / 2, imageHeight / 2, Platform.class) != null)
            isOnGround = true;
         
        return isOnGround;
    }
     
    public boolean didBumpHead() {
        boolean bumpedHead = false;
         
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneObjectAtOffset(imageWidth / -2, imageHeight / -2, Platform.class) != null ||
            getOneObjectAtOffset(imageWidth / 2, imageHeight / -2, Platform.class) != null)
            bumpedHead = true;
             
        return bumpedHead;
    }
    public boolean canMoveLeft() {
        boolean canMoveLeft = true;
         
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneObjectAtOffset(imageWidth / -2 - STEP, imageHeight / -2, Platform.class) != null ||
            getOneObjectAtOffset(imageWidth / -2 - STEP, imageHeight / 2 - 1, Platform.class) != null)
            canMoveLeft = false;
             
        return canMoveLeft;
    }
     
    public boolean canMoveRight() {
        boolean canMoveRight = true;
         
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneObjectAtOffset(imageWidth / 2 + STEP, imageHeight / -2, Platform.class) != null ||
            getOneObjectAtOffset(imageWidth / 2 + STEP, imageHeight / 2 - 1, Platform.class) != null)
            canMoveRight = false;
             
        return canMoveRight;
    }
}
Lastly there's a video summarising it (feel free to pause): Click Here
danpost danpost

2019/1/11

#
The_Beast0007 wrote...
when I try to run it the greenfoot terminal pops up and throws an exception
Remove the first word, "Jumper ", from line 26 in your OverWorld class code.
The_Beast0007 The_Beast0007

2019/1/11

#
!AAARGH!! IS IT REALLY THAT SIMPLE I HAD BEEN TRYING TO FIGURE IT OUT FOR OVER 3 HOURS!!!!!!! YAY! But thank you once again danpost for saving me when I was going to uninstall greenfoot.
You need to login to post a reply.