-1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
joshua.vw
  • 67
  • 3

2 Answers2

0

Point p1; this is null

change to this pattern of code

public void setPoint1(Point p){

    p1 = p;
}

or alternatively you could construct the points in the constructor

public Triangle2D(){ 
    p1 = new Point (-1, -1);  // or even better create a zero arg constructor
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

The p1, p2, p3 objects are null, you did not initialize them.

2 solutions :

  1. Initialize them

    public Triangle2D(){   
        p1 = new Point();
        p2 = new Point();
        p3 = new Point();
    }
    

    require to add a 0-arg constructor for Point : public Point(){}

  2. Assign the value when use the setter

    public void setPoint1(Point p){
        p1 = p;
    }
    
azro
  • 53,056
  • 7
  • 34
  • 70