I'm new to programming java and I'm attempting to find out why my code keeps giving me the error java.lang.NullPointerException. It's supposed to take 6 points, and create 2 triangles.
MAIN CLASS
public class Themain{
public static void main (String[] args){
Point pointone = new Point(1,2);
Point pointtwo = new Point(3,4);
Point pointthree = new Point(0,5);
Point josh = new Point(1,2);
Point abby = new Point(3,4);
Point trevor = new Point(0,6);
Triangle2D triangleone = new Triangle2D();
Triangle2D triangletwo = new Triangle2D();
triangleone.setPoint1(pointone);
triangleone.setPoint2(pointtwo);
triangleone.setPoint3(pointthree);
triangletwo.setPoint1(josh);
triangletwo.setPoint2(abby);
triangletwo.setPoint3(trevor);
}
}
TRIANGLE CLASS
public class Triangle2D{
Point p1;
Point p2;
Point p3;
//no args constructor
public Triangle2D(){
}
//set point one
public void setPoint1(Point p){
p1.setXPos(p.getXPos());
p1.setYPos(p.getYPos());
}
//set point two
public void setPoint2(Point p){
p2.setXPos(p.getXPos());
p2.setYPos(p.getYPos());
}
//set point three
public void setPoint3(Point p){
p3.setXPos(p.getXPos());
p3.setYPos(p.getYPos());
}
//get point one
public Point getPoint1(){
return(p1);
}
}
POINT CLASS
public class Point{
int x;
int y;
//args constructor
public Point(int x, int y){
this.x = x;
this.y = y;
}
//get the x-coordiante
public int getXPos(){
return x;
}
//set the x-coordinate
public void setXPos(int x){
this.x = x;
}
//get the y-coordinate
public int getYPos(){
return y;
}
//set the y-coordinate
public void setYPos(int y){
this.y = y;
}
//is equals method
public boolean isEquals(Point t){
return(this.x == t.x && this.y == t.y);
}
}
I'm not sure why it's giving my the null error. The real code is much longer than this but I have take the section that was causing the error and I put it into this file. I'm mostly writing this because stack overflow says there is too much code. If someone could help me understand why this error appears it would be greatly appreciated.