Turtle graphics

Im creating an array of turtles but I got this error

Exception in thread "main" java.lang.NullPointerException

at acm.graphics.GCanvas.add(GCanvas.java)

at acm.program.GraphicsProgram.add(GraphicsProgram.java)

at acm.program.GraphicsProgram.add(GraphicsProgram.java)

at TurtleRace_BK.run(TurtleRace_BK.java:18)

at acm.program.Program.runHook(Program.java)

at acm.program.Program.startRun(Program.java)

at acm.program.Program.start(Program.java)

at acm.program.Program.start(Program.java)

at acm.program.Program.main(Program.java)

what does that mean? I installed all my acm files correctly

here is part of my code

GTurtle[] turtle =new GTurtle[3];

add(turtle[0], 50, 50);

add(turtle[1], 100, 100);

add(turtle[2], 100, 200);

turtle[0].penDown();

turtle[1].penDown();

turtle[2].penDown();

[941 byte] By [darksssa] at [2007-11-26 14:38:17]
# 1

GTurtle[] turtle = new GTurtle[3];

This initializes the array of GTurtles, not each GTurtle in the array. Try something like this:

turtle[0] = new GTurtle();//<--Initialized first GTurtle

CaptainMorgan08a at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 2
when i do that it says cannot find symbolsymbol : class Gturtleturtle[1] = new Gturtle();
darksssa at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 3
Java is case sensitive, pay more attention to that.
CaptainMorgan08a at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 4
now i get this error']' expected GTurtle turtle[0] = new GTurtle();
darksssa at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 5
> now i get this error> > ']' expected GTurtle turtle[0] = new GTurtle();Don't re-declare it. Read my first response.
CaptainMorgan08a at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 6
i did exactly what you said but it give me an error
darksssa at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 7
> i did exactly what you said but it give me an errorWell I guess it's hopeless then and you should give up.Alternatively you could repost what exactly your code is now...
cotton.ma at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 8
> Alternatively you could repost what exactly your code> is now...Or he could actually do exactly what I gave him.
CaptainMorgan08a at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 9

here is what I have

GTurtle[] turtle = new GTurtle[3];

GTurtle turtle[0] = new GTurtle();

GTurtle turtle[1] = new GTurtle();

GTurtle turtle[2] = new GTurtle();

add(turtle[0], 50, 50);

add(turtle[1], 100, 100);

add(turtle[2], 100, 200);

these are the errors

C:\Documents and Settings\Admin\My Documents\TurtleRace_BK.java:15: ']' expected

GTurtle turtle[0] = new GTurtle();

^

C:\Documents and Settings\Admin\My Documents\TurtleRace_BK.java:16: ']' expected

GTurtle turtle[1] = new GTurtle();

^

C:\Documents and Settings\Admin\My Documents\TurtleRace_BK.java:17: ']' expected

GTurtle turtle[2] = new GTurtle();

^

darksssa at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 10
You dont know how much my heart ached for this to be a thread about Logo.
TuringPesta at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 11
SIGH!Cap'ns codeturtle[0] = new GTurtle();Your codeGTurtle turtle[0] = new GTurtle();Lets play "Spot the Difference".
floundera at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 12

ok so I got everything to work, now I need te turtles to move

here is what I have

public class TurtleRace_BK extends GraphicsProgram

{

public void run()

{

GTurtle[] turtle = new GTurtle[5];

int[] dy = new int[5];

for (int i = 0; i < turtle.length; i++)

{

turtle[i]= new GTurtle (50, i * 100 + 50);

add(turtle[i]);

}

for (int a = 0; a < turtle.length; a++)

{

dy = (int)(Math.random() * 70) - 50;

turtle[a].foward(dy);

}

}

}

but I get this error:

C:\Documents and Settings\Admin\My Documents\TurtleRace_BK.java:23: incompatible types

found: int

required: int[]

dy = (int)(Math.random() * 70) - 50;

^

C:\Documents and Settings\Admin\My Documents\TurtleRace_BK.java:24: cannot find symbol

symbol : method foward(int[])

location: class acm.graphics.GTurtle

turtle[a].foward(dy);

^

darksssa at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 13

C:\Documents and Settings\Admin\My Documents\TurtleRace_BK.java:23: incompatible types

found : int

required: int[]

dy = (int)(Math.random() * 70) - 50;

^

The result of this is an int but you are trying to store it in dy which is an int array. Hence incompatible types.

C:\Documents and Settings\Admin\My Documents\TurtleRace_BK.java:24: cannot find symbol

symbol : method foward(int[])

location: class acm.graphics.GTurtle

turtle[a].foward(dy);

^

The GTurtle class does not have a method called forward that has an int array as its only parameter.

floundera at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 14

>

> C:\Documents and Settings\Admin\My

> Documents\TurtleRace_BK.java:24: cannot find symbol

> symbol : method foward(int[])

> location: class acm.graphics.GTurtle

> turtle[a].foward(dy);

> ^

>

> The GTurtle class does not have a method called

> forward that has an int array as its only parameter.

And I rather suspect there is a method called forward that takes an int.

cotton.ma at 2007-7-8 8:19:19 > top of Java-index,Java Essentials,New To Java...
# 15
I fixed and everything worksthank your for you help
darksssa at 2007-7-21 16:14:32 > top of Java-index,Java Essentials,New To Java...
# 16
darksss,Please read the following http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.htmlAll your confusion in this thread has to do with arrays.
cotton.ma at 2007-7-21 16:14:32 > top of Java-index,Java Essentials,New To Java...