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

Username:   Password: 
 RegisterRegister   
 jcreator
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
RyanHB




PostPosted: Tue Oct 20, 2009 8:11 am   Post subject: jcreator

This is the client class. Below the client class I have placed the actual class which is called upon..mutators etc.. I am getting this error message:

Exception in thread "main" java.lang.NullPointerException
at Face.<init>(Face.java:19)
at Face.<init>(Face.java:27)
at SmileTest.main(SmileTest.java:29)

---------------------------------------------------------

import TerminalIO.*;
import TurtleGraphics.*;
import java.awt.Color;
import javax.swing.JColorChooser;

public class SmileTest {


public static void main(String[] args) {
KeyboardReader reader= new KeyboardReader();
double x,y, radius;
int mood=0;


//Reds in the Initial x & y Positions
x= reader.readDouble("Initial x position: ");
y= reader.readDouble ("Initial y position: ");

//Size of the FACE
radius = reader.readDouble("Set the size of the face: ");

mood= reader.readInt("1= Happy, 2= Sad: " );

// JCreator Colour pad
JColorChooser jcc= new JColorChooser();
Color c = jcc.showDialog (null, "Pick a colour for the face: ", Color.blue );

// Create the Basic Face
Face smile = new Face(x , y);
smile.setRadius(radius);
if (mood==2)
smile.setMood(false);
else
smile.setMood(true);
smile.setColor(c);
reader.pause();

smile.draw();

while (true){
x=reader.readDouble("New x position: ");
y=reader.readDouble ("New y position:" );
radius= reader.readDouble("set the size of the face: " );
mood=reader.readInt("1= Happy, 2= Sad: " );
c=jcc.showDialog(null, "Pick a new colour for the face", Color.blue);
smile.erase();
smile.setColor(c);
smile.setRadius(radius);
if (mood==2)
smile.setMood(false);
else
smile.setMood(true);
smile.move(x,y);
smile.draw();
}
}
}


-------------------------
Class with mutators etc..

import TurtleGraphics.*;
import java.awt.Color;

public class Face {
private StandardPen pen;
private double xPosition, yPosition ;
private boolean mood;
private double radius;
private Color colour;




public Face() {
xPosition = 0;
yPosition = 0;
radius= 0;
mood= true;
pen.setColor(Color.red);
pen = new StandardPen();


}


public Face (double x, double y){
this();
xPosition=x;
yPosition=y;

}

public void draw(){

//Draw the outline of the face
drawCircle(xPosition, yPosition, radius);
//Draw the left, then the right, eye.
drawCircle(xPosition - radius /2.5, yPosition +radius /3, radius /4);
drawCircle(xPosition + radius /2.5, yPosition +radius /3, radius /4);
//Draw the horizontal part of the mouth
drawLine(xPosition - radius/3, yPosition - radius/2, xPosition + radius/3, yPosition - radius/2);
if (mood==false){

//Draw the left smile line
drawLine(xPosition - radius /3, yPosition - radius/2,xPosition - radius /3 - 5, yPosition - radius/2+5);

//Draw the right smile line
drawLine(xPosition + radius /3, yPosition-radius/2,xPosition + radius /3 +5, yPosition-radius/2+5);
}else{

//Draw the left frown line
drawLine(xPosition - radius /3, yPosition - radius/2,xPosition - radius /3 - 5, yPosition - radius/2-5);

//Draw the right frown line
drawLine(xPosition + radius /3, yPosition-radius/2,xPosition + radius /3 +5, yPosition-radius/2-5);
}
}

public void erase (){
//Sets the colour to white
pen.setColor(Color.white);
//Draws so that it covers the old drawing
draw();
//Returns the new colour (white) to the old colour (whatever it was)
pen.setColor(colour);
}

public void move(double x, double y){
xPosition=x;
yPosition=y;
}

private void drawCircle (double x, double y, double r){
//Draws the face
double side= 2.0* Math.PI * r/120.0;
pen.setColor(colour);
pen.up();
pen.move(x+r, y - side/ 2.0);
pen.setDirection (90);
pen.down();
for(int i= 0; i<120; i++){
pen.move(side);
pen.turn(3);
}


}

public void drawLine (double x1, double y1, double x2, double y2){
pen.up();
pen.move(x1,y1);
pen.down();
pen.move(x2,y2);

}

public void setMood (boolean md){
mood=md;

}
public void setRadius (double rd){
//Sets the radius of the circle
radius=rd;

}
public void setColor (Color cl){
colour = cl;
pen.setColor(colour);

}
}


thanks,
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Oct 20, 2009 8:36 am   Post subject: RE:jcreator

The Face ( double x, double y ) constructor is calling the Face () constructor, which is accessing the pen variable before it's initialized. Put the initialization ( the "pen = new StandardPen()" line ) before the access to set its colour.

This is a relatively simple problem. Read the error it gives you, it shows the line numbers you need to look at and tells you more-or-less what the problem is.
RyanHB




PostPosted: Thu Oct 22, 2009 10:25 am   Post subject: RE:jcreator

Alright, that worked... The programs fixed. THanks Very Happy
Display posts from previous:   
   Index -> Programming, Java -> Java Help
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: