In: Computer Science
I keep get this exception error Exception in thread "main"
java.lang.NullPointerException
at
Quadrilateral.returnCoordsAsString(Quadrilateral.java:44)
at Quadrilateral.toString(Quadrilateral.java:51)
at tester1.main(tester1.java:39)
In this program I needed to make a Point class to create a coordinate square from x and y. I also needed to make a Quadrilateral class that has an instance reference variable to Point
.The Quadrilateral class then inherits itself to other classes or in this case other shapes like square, trapazoid. I thought I did it right but for some reason there seems to be a problem with the toString class.
Here is Point class
public class Point {
private double x;
private double y;
public Point(double point1, double point2)
{
x = point1;
y = point2;
}
public Point()
{
x = 0.00;
y = 0.00;
}
public void setx(double x)
{
this.x = x;
}
public void sety(double y)
{
this.y = y;
}
public double getx()
{
return x;
}
public double gety()
{
return y;
}
@Override
public String toString()
{
return "(" + getx() + " , " + gety() + ")";
}
}
here is the Quadrilateral class
public class Quadrilateral {
Point Corner1;
Point Corner2;
Point Corner3;
Point Corner4;
public Quadrilateral(double x1, double y1, double x2, double y2,
double x3, double y3 , double x4, double y4)
{
Point Corner1 = new Point(x1, y1);
Point Corner2 = new Point(x2, y2);
Point Corner3 = new Point(x3, y3);
Point Corner4 = new Point(x4, y4);
}
public Point getCorner1()
{
return Corner1;
}
public Point getCorner2()
{
return Corner2;
}
public Point getCorner3()
{
return Corner3;
}
public Point getCorner4()
{
return Corner4;
}
public String returnCoordsAsString()
{
String name1 = getCorner1().toString() + " , " +
getCorner2().toString() + " , " + getCorner3().toString() + " , " +
getCorner4().toString();
return name1;
}
@Override
public String toString()
{
String name = " coordinates od Quadrilateral are : " +
returnCoordsAsString();
return name;
}
}
and this is the diver class
public class tester1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Quadrilateral quadrilateral = new Quadrilateral(
1.1, 1.2, 6.6, 2.8, 6.2, 9.9, 2.2, 7.4 );
quadrilateral.toString();
}
}
If you have any doubts, please give me comment...
Point.java
public class Point {
private double x;
private double y;
public Point(double point1, double point2) {
x = point1;
y = point2;
}
public Point() {
x = 0.00;
y = 0.00;
}
public void setx(double x) {
this.x = x;
}
public void sety(double y) {
this.y = y;
}
public double getx() {
return x;
}
public double gety() {
return y;
}
@Override
public String toString() {
return "(" + getx() + " , " + gety() + ")";
}
}
Quadralateral.java
public class Quadrilateral {
Point Corner1;
Point Corner2;
Point Corner3;
Point Corner4;
public Quadrilateral(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
Corner1 = new Point(x1, y1);
Corner2 = new Point(x2, y2);
Corner3 = new Point(x3, y3);
Corner4 = new Point(x4, y4);
}
public Point getCorner1() {
return Corner1;
}
public Point getCorner2() {
return Corner2;
}
public Point getCorner3() {
return Corner3;
}
public Point getCorner4() {
return Corner4;
}
public String returnCoordsAsString() {
String name1 = getCorner1().toString() + " , " + getCorner2().toString() + " , " + getCorner3().toString()
+ " , " + getCorner4().toString();
return name1;
}
@Override
public String toString() {
String name = " coordinates od Quadrilateral are : " + returnCoordsAsString();
return name;
}
}
tester1.java
public class tester1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Quadrilateral quadrilateral = new Quadrilateral(1.1, 1.2, 6.6, 2.8, 6.2, 9.9, 2.2, 7.4);
System.out.println(quadrilateral.toString());
}
}