2011-12-08 59 views
0

我正在編寫一個程序,我希望我的對象「this」將是一個Point數組,但是當我運行該程序時出現此錯誤,但我不明白爲什麼。 錯誤 - > DouglasPeucker。 我PROGRAMM:Array Point的構造函數

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

有什麼錯誤? – mre

回答

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

或者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 
} 
+0

你比我快得多! +1 – Jomoos

+0

哦,這個愚蠢的錯誤!謝謝您的幫助 ! – afk