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

2013/1/9

Constructor Errors

1
2
Gingervitis Gingervitis

2013/1/9

#
I am making a game with various animals and when i try to add a new class (which the player controls), it says that the constructor has not been defined yet every other animal works. Why is that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
  
*  Create a new Turtle and place it randomly
   */
  public void createTurtle()
  {
      Turtle newTurtle;
      newTurtle = new Turtle();  // The error is in this line where it says new Turtle();
 
      World world;
      world = getWorld();
 
      int worldWidth = world.getWidth();
      int worldHeight =world.getHeight();
 
      int x = Greenfoot.getRandomNumber(worldWidth);
      int y = Greenfoot.getRandomNumber(worldHeight);
      world.addObject(newTurtle, x, y);
  }
danpost danpost

2013/1/9

#
You will have to show your Turtle class for help on this (the whole class).
Gingervitis Gingervitis

2013/1/9

#
Do you mean to show my entire class for turtle? Because i have a lot of stuff....
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import greenfoot.*;
 
public class Turtle extends Animal
{
    private Counter counter;
    public Turtle(Counter pointCounter)
    {
        counter = pointCounter;
    }
    int x =Greenfoot.getRandomNumber(4) + 4;
    // if you want to have the turtle move at a random speed,
    //switch "4" in move to the int variable "x"
    public void act()
    {
        move(4);
         
        tryToEat();
        tryToEatStarfish();
        tryToEatCrab();
        ifAtEdge();
        tryToEatUD();
        tryToEatSUD();
         
     
     
        if (Greenfoot.mouseClicked(this))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();          
             
         createTurtle();
        }
     
         
    }
 
    /**
     * If turtle reaches edge of world, turn (x) amount
     */
    public void ifAtEdge()
    {
        if ( atWorldEdge() )
        {
            if(canSee(Destroyer.class))
            {
                eat(Destroyer.class);
                counter.add(150);
                Greenfoot.playSound("slurp.wav");
                createNewDestroyer2();
                createNewUltimateDestroyer();
                createNewCrab();
                createNewCrab();
                createNewStarfish();
            }
            if (canSee(SuperUltimateDestroyer.class))
            {
                eat(SuperUltimateDestroyer.class);
                Greenfoot.playSound("slurp.wav");
                counter.add(750);
                gameOver();
                 
            }
 
        }
    }
 
    public void tryToEatSUD()   
    {
        if (canSee(SuperUltimateDestroyer.class))
        {
            eat(SuperUltimateDestroyer.class);
            Greenfoot.playSound("slurp.wav");
            counter.add(750);
            gameOver();
        }
    }
 
    public void tryToEatUD()
    {
        if(canSee(UltimateDestroyer.class))
        {
            eat(UltimateDestroyer.class);
            counter.add(500);
            Greenfoot.playSound("slurp.wav");
            createNewSuperUltimateDestroyer();
            move(10);
        }
    }
 
     
 
    /**
     * If turtle can see lettuce or Destroyer and sometimes a snake,
     * it will eat them
     */
    public void tryToEat()
    {
        if(canSee(Lettuce.class))
        {
            eat(Lettuce.class);
            counter.add(5);
            Greenfoot.playSound("slurp.wav");
 
        }
        if(canSee(Bug.class))
        {
            eat(Bug.class);
            counter.add(20);
            Greenfoot.playSound("slurp.wav");
            createNewBug();
            createNewBug();
            createNewSnake();
            if(canSee(Destroyer.class))
            {
                eat(Destroyer.class);
                counter.add(150);
                createNewDestroyer2();
                createNewUltimateDestroyer();
                createNewCrab();
                createNewCrab();
                createNewStarfish();
            }
        }
        if (canSee(Snake.class))
        {
            eat(Snake.class);
            counter.add(75);
            createNewDestroyer();
            if(canSee(Destroyer.class))
            {
                eat(Destroyer.class);
                counter.add(150);
                createNewDestroyer2();
                createNewUltimateDestroyer();
                createNewCrab();
                createNewCrab();
            }
        }
        if (canSee(Counter.class))
        {
            if(canSee(Destroyer.class))
            {
                eat(Destroyer.class);
                counter.add(150);
                createNewDestroyer2();
                createNewUltimateDestroyer();
                createNewCrab();
                createNewCrab();
 
            }
            if(canSee(Starfish.class))
            {
                eat(Starfish.class);
                counter.add(5);
                Greenfoot.playSound("slurp.wav");
                createNewStarfish();
                createNewCrab();
                createNewCrab();
            }
 
            if (canSee(Crab.class))
            {
                eat(Crab.class);
                counter.add(75);
                createNewCrab();
                createNewStarfish();
                createNewStarfish();
            }
            if (counter.getValue() == 1000  )
            {
                gameOver();
            }
        }
    }
 
    public void tryToEatStarfish()
    {
        if(canSee(Starfish.class))
        {
            eat(Starfish.class);
            createNewStarfish();
            createNewCrab();
        }
    }
 
    public void tryToEatCrab()
    {
        if (canSee(Crab.class))
        {
            eat(Crab.class);
            createNewCrab();
            createNewStarfish();
        }
    }
 
    /**
     * We won the game.
     */
    public void gameOver()
    {
        Greenfoot.playSound("gamefinish.wav");
        Greenfoot.stop();
    }
 
    /**
     *  Create a new bug and place it randomly
     */
    private void createNewBug()
    {
        Bug newBug;
        newBug = new Bug();
 
        World world;
        world = getWorld();
 
        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();
 
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newBug, x, y);
    }
 
    /**
     *  Create a new snake and place it randomly
     */
    private void createNewSnake()
    {
        Snake newSnake;
        newSnake = new Snake();
 
        World world;
        world = getWorld();
 
        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();
 
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newSnake, x, y);
    }
 
    /**
     *  Create a new animal that eats everything but you and place it randomly
     */
    private void createNewDestroyer()
    {
        Destroyer newDestroyer;
        newDestroyer = new Destroyer();
 
        World world;
        world = getWorld();
 
        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();
 
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newDestroyer, x, y);
    }
 
    /**
     *  Create a new animal that eats everything but you and place it randomly
     */
    private void createNewDestroyer2()
    {
        Destroyer2 newDestroyer2;
        newDestroyer2 = new Destroyer2();
 
        World world;
        world = getWorld();
 
        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();
 
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newDestroyer2, x, y);
    }
 
    /**
     *  Create a new bug and place it randomly
     */
    private void createNewUltimateDestroyer()
    {
        UltimateDestroyer newUltimateDestroyer;
        newUltimateDestroyer = new UltimateDestroyer();
 
        World world;
        world = getWorld();
 
        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();
 
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newUltimateDestroyer, x, y);
    }
 
    /**
     *  Create a new starfish and place it randomly
     */
    private void createNewStarfish()
    {
        Starfish newStarfish;
        newStarfish = new Starfish();
 
        World world;
        world = getWorld();
 
        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();
 
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newStarfish, x, y);
    }
 
    /**
     *  Create a new crab and place it randomly
     */
    private void createNewCrab()
    {
        Crab newCrab;
        newCrab = new Crab();
 
        World world;
        world = getWorld();
 
        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();
 
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newCrab, x, y);
    }
 
    /**
     *  Create a new bug and place it randomly
     */
    private void createNewSuperUltimateDestroyer()
    {
        SuperUltimateDestroyer newSuperUltimateDestroyer;
        newSuperUltimateDestroyer = new SuperUltimateDestroyer();
 
        World world;
        world = getWorld();
 
        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();
 
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newSuperUltimateDestroyer, x, y);
    }
        /**
     *  Create a new Turtle and place it randomly
     */
    public void createTurtle()
    {
        Turtle newTurtle;
        newTurtle = new Turtle();
 
        World world;
        world = getWorld();
 
        int worldWidth = world.getWidth();
        int worldHeight =world.getHeight();
 
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);
        world.addObject(newTurtle, x, y);
    }
    }
danpost danpost

2013/1/9

#
Yes, but in all that code, you do not have a constructor without any parameters. You only have the one that takes a Counter object. So trying to create one with 'new Turtle()' without a Counter object supplied within the parenthesis will cause this error.
Gingervitis Gingervitis

2013/1/9

#
I tried that before and it didn't work. Let me try it again.
Gingervitis Gingervitis

2013/1/9

#
I just did what you told be to do, but now I have a new problem.... I have no control of the turtle now. Any suggestions?
Gingervitis Gingervitis

2013/1/9

#
I think I know what do for that issue.
Gingervitis Gingervitis

2013/1/9

#
Thanks for the error help. I appreciate it. I just started Greenfoot over the weekend.
danpost danpost

2013/1/9

#
I really did not say what to do about it, the choice was up to you. Either (1) create a new constructor in the Turtle class that does not contain a Counter parameter or (2) add a Counter to the statement that calls the constructor. As I do not know which way you went, I cannot give additional advice at this time. However, should you include what you did and what control you are wanting on BOTH original and new Turtle objects, and also (for some insight as to what may be going on) a reason why you start with one Turtle object with a Counter, and when you click on that object you create another Turtle object (with or without one); and what of the original one, what happens with it?
Gingervitis Gingervitis

2013/1/9

#
it is hard to tell what happens to the other turtle because they move with my control for about 3 seconds and then the game stops due to a java error, but that is only when two or more turtles are on screen.
danpost danpost

2013/1/9

#
Do you know how to read the error message? Clear the terminal screen, run the scenario to the error, and copy/paste the message here for assistance.
Gingervitis Gingervitis

2013/1/9

#
I'm not really a computer programer. I just watched a couple of videos to learn about it more. java.lang.NullPointerException at Turtle.tryToEat(Turtle.java:120) at Turtle.act(Turtle.java:22) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
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
/**
     * If turtle can see lettuce or Destroyer and sometimes a snake,
     * it will eat them
     */
    public void tryToEat()
    {
        if(canSee(Lettuce.class))
        {
            eat(Lettuce.class);
            counter.add(5);
            Greenfoot.playSound("slurp.wav");
 
        }
        if(canSee(Bug.class))
        {
            eat(Bug.class);
            counter.add(20);
            Greenfoot.playSound("slurp.wav");
            createNewBug();
            createNewBug();
            createNewSnake();
        }
        if(canSee(Destroyer.class))
        {
            eat(Destroyer.class);
            counter.add(150);
            createNewDestroyer2();
            createNewUltimateDestroyer();
            createNewCrab();
            createNewCrab();
            createNewStarfish();
        }
 
        if (canSee(Snake.class))
        {
            eat(Snake.class);
            counter.add(75);
            createNewDestroyer();
        }
        if(canSee(Destroyer.class))
        {
            eat(Destroyer.class);
            counter.add(150);
            createNewDestroyer2();
            createNewUltimateDestroyer();
            createNewCrab();
            createNewCrab();
        }
     
    if (canSee(Counter.class))
    {
        if(canSee(Destroyer.class))
        {
            eat(Destroyer.class);
            counter.add(150);
            createNewDestroyer2();
            createNewUltimateDestroyer();
            createNewCrab();
            createNewCrab();
 
        }
        if(canSee(Starfish.class))
        {
            eat(Starfish.class);
            counter.add(5);
            Greenfoot.playSound("slurp.wav");
            createNewStarfish();
            createNewCrab();
            createNewCrab();
        }
 
        if (canSee(Crab.class))
        {
            eat(Crab.class);
            counter.add(75);
            createNewCrab();
            createNewStarfish();
            createNewStarfish();
        }
        if (counter.getValue() == 1000  )
        {
            gameOver();
        }
    }
}
public void act()
    {
        move(4);
 
        tryToEat();
        tryToEatStarfish();
        tryToEatCrab();
        ifAtEdge();
        tryToEatUD();
        tryToEatSUD();
        checkKeys();
        mouseClick();
    }
danpost danpost

2013/1/9

#
at Turtle.tryToEat(Turtle.java:120) That is the line that tells you where the error took place -Turtle class - tryToEat method - line 120 in the class Which line is line 120 in the Turtle class code (you can display line numbers by Edit>Preferences /Editor tab/'Display line number' checkbox). I fear it is the line 'counter.add(5);' because you do not have a counter assigned to the Counter field for the new Turtle object. The error occurs the moment the new Turtle object finds a piece of lettuce.
Gingervitis Gingervitis

2013/1/9

#
It says there is an error, but i don't see anything that needs to be changed.
danpost danpost

2013/1/9

#
Let me explain something. The initial Turtle object you create was given a Counter object to work with (I would like to know if it was added to the world or not; though this does interfere with the workings thereof). The new Turtle object was NOT given a Counter object to work with, so its 'counter' field is still 'null' (does not contain a Counter object). When the new Turtle object finds lettuce, your code tries to add 5 points to the non-existent counter and causes an error (because 'counter' is 'null'; hence NullPointerException). The easiest fix is to change the line
1
2
3
counter.add(5);
// TO
if(counter != null) counter.add(5);
But, then the second Turtle object will still not have a Counter object to work with. If you want a new Counter object to be assigned to the new Turtle object, just create a new one before creating the new Turtle object and pass it in the parameter of the constructor call for the new Turtle.
There are more replies on the next page.
1
2