我正在开发一个程序,在该程序中我的对象“ 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);
    }
}

最佳答案

您永远不会为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
}

09-26 14:48