Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 java.lang.NullPointerException when I create an object using my Turtle class?
Index -> General Discussion
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Kratos321




PostPosted: Thu Mar 01, 2012 5:19 pm   Post subject: java.lang.NullPointerException when I create an object using my Turtle class?

Hey everyone, so this is my Turtle class:
(by the way, I am using the "Ready to Program with Java IDE" with the hsa console library methods)

// The "Turtle" class.
import java.awt.*;
import hsa.Console;

public class Turtle
{
static Console c;
protected int x, y, angle;
protected boolean showing = true;
protected Color clr = Color.black;


//Constructor for default initial values of position and angle.

public Turtle (Console C)
{

this.c = c;
x = c.getWidth () / 2;
y = c.getHeight () / 2;
angle = 0;
} //Default Turtle constructor.


//Alternate constructor.
public Turtle (Console C, int x, int y, int angle)
{
this.c = c;
this.x = x;
this.y = y;
this.angle = angle;

} //Alternate Turtle constructor


//Other methods of Turtle class

public void setColor (Color clr)
{
this.clr = clr;
} //setColor method


public void setPosition (int x, int y)
{
this.x = x;
this.y = y;
} //setPosition method


public void setAngle (int angle)
{
this.angle = angle;
} //setAngle method


public void turnLeft (int turnAngle)
{
angle += turnAngle;
angle = angle % 360;
} //turnLeft method


public void turnRight (int turnAngle)
{
angle -= turnAngle;
angle = angle % 360;
} //turnRight method


public void showTrace ()
{
showing = true;
} //showTrace method


public void hideTrace ()
{
showing = false;
} //hideTrace method


public void move (int distance)
{
int newx, newy;
double rAngle = (angle * Math.PI) / 180;
c.println (angle);
newx = (int) Math.round (x + Math.cos (rAngle) * distance);
newy = (int) Math.round (y - Math.cos (rAngle) * distance);
if (showing)
{
c.setColor (clr);
c.drawLine (x, y, newx, newy);
}
x = newx;
y = newy;
} //move method
} // Turtle class


okay and this is my separate java file where i use the above class:

// The "UseTurtle" class.
import java.awt.*;
import hsa.Console;

public class UseTurtle
{
static Console c; // The output console

public static void main (String[] args)
{
c = new Console ();

Turtle t = new Turtle(c);


} // main method
} // UseTurtle class

all i did was create the object. when i run the program, I get the following error:

java.lang.NullPointerException
at Turtle.<init>(Turtle.java:19)
at UseTurtle.main(UseTurtle.java:13)


any help please? Sad
Sponsor
Sponsor
Sponsor
sponsor
Yves




PostPosted: Thu Mar 01, 2012 5:30 pm   Post subject: Re: java.lang.NullPointerException when I create an object using my Turtle class?

Quote:
public Turtle (Console C)
{

this.c = c;

Notice your parameter is C, while you set this.c to c.
Kratos321




PostPosted: Thu Mar 01, 2012 5:34 pm   Post subject: RE:java.lang.NullPointerException when I create an object using my Turtle class?

oh WOW! Thanks alot!
Display posts from previous:   
   Index -> General Discussion
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: