my code is as follows:

class pottertr_Point{
private String name;
private double x;
private double y;
public pottertr_Point(){
name="Org";
x=(0);
y=(0);
}
public pottertr_Point getPoint(){
    pottertr_Point p= new pottertr_Point();
    p.name=this.name;
    p.x=this.x;
    p.y=this.y;
    return p ;

}
public void setPoint(pottertr_Point p){
    this.name=p.name;
    this.x=p.x;
    this.y=p.y;

}
public void setName(String name){
    this.name=name;

}
public void setX(double x){
    this.x=x;
}
public void setY(double y){
    this.y=y;

}
public String getName(){
    return name;
}
public double getX(){
    return x;
}
public double getY(){
    return y;
}

public double distance(pottertr_Point p){
    double distance;
    distance= Math.sqrt((this.x-p.x)*(this.x-p.x)+(this.y-p.y)*(this.y-p.y));
    return distance;

}

}

public class Program5{
    public static void main(String[] args){
    pottertr_Point [] p =new pottertr_Point[3];
    //pottertr_Point pp=new pottertr_Point();
    String s=new String ("");
    //p[0].setName(pp);

        for (int i=0; i<3;i++){
        s="p"+i;
        p[i].setName(s);
        p[i].setX(getCoordinate());
        p[i].setY(getCoordinate());
        System.out.println(p[i].getName()+", coordinate: (" + p[i].getX() + "," + p[i].getY());

    }
        for (int i=0;i<3;i++){
        for (int j=i+1;j<3;j++){
            System.out.println(p[i].getName()+ p[j].getName()+"distance"+p[i].distance(p[j])); 
        }
    }

    }   

    public static double getCoordinate(){
    double r;
        r=((int)(Math.random()*1.1))*10;
    return r;   
    }

}

the error is at line 66 which is p[i].setName(s);
the error is java.lang.NullPointerException
any help will be useful. thank you.

You need something like below on or around line 57

    p[0] = new pottertr_Point();
    p[1] = new pottertr_Point();
    p[2] = new pottertr_Point();

take this line
//pottertr_Point pp=new pottertr_Point();

out of comment, and use this instance:

for (int i=0; i<3;i++){
        s="p"+i;
        pp.setName(s);
        pp.setX(getCoordinate());
        pp.setY(getCoordinate());
        p[i] = pp;
        System.out.println(p[i].getName()+", coordinate: (" + p[i].getX() + "," + p[i].getY());
    }

is there a reason why both x and y should have the same value? Remove some of those blank lines in your code, and try and keep a logical indentation, it'll make your code easier to read.

This article has been dead for over six months. Start a new discussion instead.