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

2018/1/25

I am trying to make a score counter but get a java.lang.NullPointerException

1
2
Timmy_11 Timmy_11

2018/1/25

#
My Crab code is:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
 
/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Player1 extends Animal
{
     
    public void act()
    {
        if(Greenfoot.isKeyDown("up"))
        {
           move(getRandomNumber(1,3));
        }
        if(Greenfoot.isKeyDown("down"))
        {
           move(getRandomNumber(-3,-1));
        }
        if(Greenfoot.isKeyDown("left"))
        {
           turn(getRandomNumber(-5,-3));
        }
        if(Greenfoot.isKeyDown("right"))
        {
           turn(getRandomNumber(3,5));
        }
        lookForWorm(); // java.lang.NullPointerException
 
         
     
    }
    public void lookForWorm()
    {
        if(canSee(Worm.class))
        {
            eat(Worm.class);
            CrabWorld world = (CrabWorld) getWorld();
            Score1 score1 = world.getCounter1();
            score1.addScore1(); // java.lang.NullPointerException
        }
    }
    public int getRandomNumber(int start, int end)
    {
        int normal = Greenfoot.getRandomNumber(end-start+1);
        return normal+start;
    }
}
Then the Counter code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Score here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Score1 extends Animal
{
    private int score1 = 0;
    public Score1()
    {
        setImage(new GreenfootImage("Player1 Score : " + score1, 24, Color.GREEN, Color.BLACK));
    }
    public void addScore1()
    {
        score1 ++;
        setImage(new GreenfootImage("Player1 Score : " + score1, 24, Color.GREEN, Color.BLACK));
    }
}
And I want to make it count every time i eat a worm I marked the places with a comment in the crab code were the java.lang.NullPointerException occurred can i have help fixing it?? Thanks.
Vercility Vercility

2018/1/25

#
Show your world code. You probably didnt initialize the Counter1 there so getCounter() returns null.
Timmy_11 Timmy_11

2018/1/25

#
World Code is:
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
import greenfoot.*;  // (Actor, World, Greenfoot, GreenfootImage)
import java.util.List;
 
public class CrabWorld extends World
{
     
    /**
     * Create the crab world (the beach). Our world has a size
     * of 560x560 cells, where every cell is just 1 pixel.
     */
    public CrabWorld()
    {
        super(560, 560, 1);
    }
    boolean SetUp = false;
    int DudesAlive = 2;
    int Player1out = 0;
    int Player2out = 0;
    private Score1 theCounter1;
    public void act()
    {
         
        if(SetUp==false)
        {
          setUp(); 
          SetUp = true;
        }
        if (getObjects(Worm.class).size() <3) addWorms();
        if (getObjects(Player1.class).isEmpty())
        {
            if (Player1out == 0)
            {
                 DudesAlive = DudesAlive - 1;
                 Player1out = 1;
            }
        }
        if (getObjects(Player2.class).isEmpty())
        {
            if (Player2out == 0)
            {
                 DudesAlive = DudesAlive - 1;
                 Player2out = 1;
            }           
        }
        if (DudesAlive==0)
        {
            END();
        }
    }
    private void addWorms()
    {
        int randX = Greenfoot.getRandomNumber(560);
        int randY = Greenfoot.getRandomNumber(560);
        Worm worm = new Worm();
        addObject(worm, randX, randY);
    }
    private void setUp()
    {
        int DudesAlive = 2;
        int randX = Greenfoot.getRandomNumber(560);
        int randY = Greenfoot.getRandomNumber(560);
        Player1 crab = new Player1();
        addObject(crab, randX, randY);
        int randX2 = Greenfoot.getRandomNumber(560);
        int randY2 = Greenfoot.getRandomNumber(560);
        Player2 crab2 = new Player2();
        addObject(crab2, randX2, randY2);
        int randX3 = Greenfoot.getRandomNumber(560);
        int randY3 = Greenfoot.getRandomNumber(560);
        Lobster lobster = new Lobster();
        addObject(lobster, randX3, randY3);
        Score1 score1 = new Score1();
        addObject(score1, 90, 20);
    }
    public void END()
    {
        removeObjects(getObjects(Player2.class));
        removeObjects(getObjects(Worm.class));
        removeObjects(getObjects(Lobster.class));
        GameOver gameover = new GameOver();
        addObject(gameover, 280, 280);
        Greenfoot.stop();
    }
    public Score1 getCounter1()
    {
        return theCounter1;
    }
   
     
}
Vercility Vercility

2018/1/25

#
You forgot to put setup() into the constructor, so counter 1 stays null
Timmy_11 Timmy_11

2018/1/25

#
if(SetUp==false) { setUp(); SetUp = true; } is in the act() part is that what it should be???
Vercility Vercility

2018/1/26

#
Oh. I missed that. If you want something to be executed once only, just put it into the constructor though. I'm not quite sure on how the order of act() executes is in greenfoot. If putting it into the constructor doesn't solve the problem Reply again and I'll look into it
Timmy_11 Timmy_11

2018/1/26

#
it sets up fine because it will only execute once with the if statement but i get the java.lang.NullPointerException when i run into a worm with the crab and it tries to add a point. if i take the CrabWorld world = (CrabWorld) getWorld(); Score1 score1 = world.getCounter1(); score1.addScore1(); out of the lookForWorm() method it eats the worms with out an issue so it is something with adding the point to score1 by the way how would i put it in the constructor because if i put it there it thinks im tri=ying to make a new method or something
Vercility Vercility

2018/1/26

#
Just put setup() in public Crabworld and remove the if statement out of act You never initialize theCounter1 in setup() Should be theCounter1 = new Score instead of score1
Timmy_11 Timmy_11

2018/1/26

#
by the boolean SetUp = false; int DudesAlive = 2; int Player1out = 0; int Player2out = 0; private Score1 theCounter1; and add setUp() to the bottom setUp(); like so?? because it says invalid method declaration; return type required
Vercility Vercility

2018/1/26

#
No... Hahah The constructor is this
1
2
3
4
public Crabworld(){
Super(...);
SetUp() // <--- Here
}
Timmy_11 Timmy_11

2018/1/26

#
k thanks that works but i dont get what you mean about the theCounter1 = new Score instead of score1 in my setUp() method it says Score 1 score1 = new Score1(); addObject(score1, 90, 20); this is just to add it to the screen
Vercility Vercility

2018/1/26

#
Your getCounter1 method returns theCounter1 but you never created it, you only declared that it exists with private Score1 theCounter1 Rather than creating a new Score1, create theCounter1 with
1
2
theCounter1 = new Score1;
addObject(theCounter1, 90, 20);
Timmy_11 Timmy_11

2018/1/26

#
where should i put that should i just float it in by all the state variables?
Timmy_11 Timmy_11

2018/1/26

#
nevermind you did give me the right stuff and now the score works!!!
Timmy_11 Timmy_11

2018/1/26

#
thanks so much ;)
There are more replies on the next page.
1
2