afk
afk

Reputation: 633

Constructor of Array Point

I'm working on a programm in which I want my object "this" will be an array of Point but I have this error when I run the programm and I'm not understand why. Error --> DouglasPeucker. My programm :

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);
    }
}

Upvotes: 0

Views: 123

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1501656

You're never assigning a value to coinImage[i], so it will have its default value of null, which you're the dereferencing. You need something like:

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);
}

Or preferrably, 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
}

Upvotes: 3

Related Questions