Array Point的构造函数

时间:2011-12-08 18:37:52

标签: java arrays point

我正在编写一个程序,我希望我的对象“this”将是一个Point数组,但是当我运行程序时我有这个错误,我不明白为什么。 错误 - > DouglasPeucker 我的程序:

public class DouglasPeucker {

private double epsilon; 
protected Point[] coinImage;

public DouglasPeucker(Point [] tab) {
    this.coinImage = new Point[tab.length];
    for(int i = 0; i < this.coinImage.length; i++) {
        double abscisse = tab[i].getX();
        double ordonnee = tab[i].getY();
        System.out.println(abscisse + " " + ordonnee);
        this.coinImage[i].setX(abscisse);
        this.coinImage[i].setY(ordonnee);
    }
}

1 个答案:

答案 0 :(得分:3)

您永远不会为coinImage[i]分配值,因此它的默认值为null,您可以取消引用它。你需要这样的东西:

for(int i = 0; i < this.coinImage.length; i++) {
    double abscisse = tab[i].getX();
    double ordonnee = tab[i].getY();
    System.out.println(abscisse + " " + ordonnee);
    this.coinImage[i] = new Point();
    this.coinImage[i].setX(abscisse);
    this.coinImage[i].setY(ordonnee);
}

或者最好是,IMO:

for (int i = 0; i < this.coinImage.length; i++) {
    // I'm assuming Point has a sensible constructor here...
    coinImage[i] = new Point(tab[i].getX(), tab[i].getY());
    // Insert the diagnostics back in if you really need to
}