使用多个类中的方法时出现java.lang.NullPointerException [重复]

[英]java.lang.NullPointerException when using methods from multiple classes [duplicate]


This question already has an answer here:

这个问题在这里已有答案:

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.

我是编程java的新手,我试图找出为什么我的代码一直给我错误java.lang.NullPointerException。它应该占6分,并创建2个三角形。

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.

我不确定为什么它会给出我的空错误。真正的代码比这长得多,但我已经采取导致错误的部分,并将其放入此文件中。我主要是写这个,因为堆栈溢出说代码太多了。如果有人可以帮助我理解为什么会出现此错误,我们将不胜感激。

2 个解决方案

#1


0  

Point p1; this is null

点p1;这是空的

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
}

#2


0  

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

p1,p2,p3对象为null,您没有初始化它们。

2 solutions :

2解决方案:

  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(){}

    需要为Point添加一个0-arg构造函数:public Point(){}

  2. Assign the value when use the setter

    使用setter时分配值

    public void setPoint1(Point p){
        p1 = p;
    }
    

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/10/29/2f8e311c32fe9c11ba1a211b6bcca152.html



 
  © 2014-2022 ITdaan.com 联系我们: