使用多個類中的方法時出現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 联系我们: