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]

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
when i do that it says cannot find symbolsymbol : class Gturtleturtle[1] = new Gturtle();
Java is case sensitive, pay more attention to that.
now i get this error']' expected GTurtle turtle[0] = new GTurtle();
> now i get this error> > ']' expected GTurtle turtle[0] = new GTurtle();Don't re-declare it. Read my first response.
i did exactly what you said but it give me an error
> 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...
> Alternatively you could repost what exactly your code> is now...Or he could actually do exactly what I gave him.
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();
^
You dont know how much my heart ached for this to be a thread about Logo.
SIGH!Cap'ns codeturtle[0] = new GTurtle();Your codeGTurtle turtle[0] = new GTurtle();Lets play "Spot the Difference".
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);
^
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.
>
> 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.
I fixed and everything worksthank your for you help
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.