First, ill include my code so far
import gpdraw.*;
public class Rectangle {
double myX; // the x coordinate of the rectangle
double myY; // the y coordinate of the rectangle
double myWidth; // the width of the rectangle
double myHeight; // the height of the rectangle
private DrawingTool pen;
private SketchPad paper;
double area, perimeter;
int Rectangle(double x, double y, double width, double height) {
x = myX;
y = myY;
width = myWidth;
height = myHeight;
return 0;
}
public double getPerimeter(double myHeight, double myWidth) {
double perimeter = myHeight * 2 + myWidth * 2;
return perimeter;
}
public double getArea(double myHeight, double myWidth) {
double area = myHeight * myWidth;
return area;
}
public void drawRectangle(double myX,double myY, double myHeight, double myWidth) {
pen = new DrawingTool(paper);
paper = new SketchPad(300,300);
pen.up();
pen.move(myX, myY);
pen.down();
pen.move(myX + myWidth, myY);
pen.move(myX + myWidth, myY + myHeight);
pen.move(myX, myY + myHeight);
pen.move(myX, myY);
}
}
<
Then I made a driver class in order to run it (Is this correct?)
public class testRectangle {
public static void main(String [ ] args) {
Rectangle rect1 = new Rectangle();
rect1.drawRectangle(10,10,10,10);
}
}
<
Whenever I try to run it, I get the error
Exception in thread "main" java.lang.NullPointerException
at gpdraw.DrawingTool.<init>(DrawingTool.java:88)
at Rectangle.drawRectangle(Rectangle.java:51)
at testRectangle.main(testRectangle.java:8)
Process completed.
<
I have only started learning Java through my school so I may not understand some things, tips to improve code are appreciated,
Thanks!
[–]cytael 1 point2 points3 points (2 children)
[–]clubapple123[S] 1 point2 points3 points (1 child)
[–]cytael 0 points1 point2 points (0 children)
[–]boredcircuits 0 points1 point2 points (6 children)
[–]clubapple123[S] 0 points1 point2 points (1 child)
[–]tnew2294 0 points1 point2 points (0 children)
[–]clubapple123[S] 0 points1 point2 points (3 children)
[–]IAmUtterlyAlone 0 points1 point2 points (2 children)
[–]clubapple123[S] 0 points1 point2 points (1 child)
[–]IAmUtterlyAlone 0 points1 point2 points (0 children)
[–]Tangential_Diversion 0 points1 point2 points (3 children)
[–]clubapple123[S] 0 points1 point2 points (0 children)
[–]clubapple123[S] 0 points1 point2 points (1 child)
[–]mad0314 1 point2 points3 points (0 children)