尝试设置变量时,Point []数组会抛出NullPointerException

时间:2014-09-08 17:52:29

标签: java arrays netbeans-7.4

好。我已经尝试了一切,而且我一直在寻找互联网上的答案,包括这个网站,但是......根本没有运气。

这是我的代码:

package com.exercises.algorithms;
import java.awt.*;


public class Points2DN {
    private Point[] points;
    protected int[] closestPoints = new int[2];
    private double distance;

Points2DN(int n) {
    this.points = new Point[n];
    for (int i = 0; i < n; i++) {
        this.points[i].x =  (int) Math.floor(Math.random()*100);
        this.points[i].y =  (int) Math.floor(Math.random()*100);
        System.out.println("Test: " + points[i].x + "," + points[i].y + "\n");
        System.out.println("OK");
    }
    this.distances(this);
}


public static void imprimePuntos(Point[] arrayPuntos) {
    for (int i = 0; i < arrayPuntos.length; i++) {
        System.out.println("Punto " +(i+1) + ": " + "( "
                + arrayPuntos[i].x + " , "
                + arrayPuntos[i].y + " )");
    }
}

public static void printClosest(Points2DN puntos) {
    System.out.println("\nLos puntos más cercanos son: ");
    System.out.println("Puntos: \t" 
            + (puntos.points[puntos.closestPoints[0]]).toString() + " "
            + (puntos.points[puntos.closestPoints[1]]).toString());
    System.out.println("Distancia: " + puntos.distance);


}

private void distances(Points2DN puntos) {
    for (int i = 0; i < puntos.points.length; i++) {
            // Iterative loop for the other points to be check with
        for (int j = (i+1); j < this.points.length; j++) {
            if (i == 0 && j == 1) {
                int p = puntos.closestPoints[0] = i;
                int q = puntos.closestPoints[1] = j;
                puntos.distance = (puntos.points[p]).distance(puntos.points[q]);
                continue;
            }
            double distancePair = puntos.points[i].distance(puntos.points[j]);
            if (distancePair < puntos.distance){
                puntos.closestPoints[0] = i;
                puntos.closestPoints[1] = j;
                puntos.distance = distancePair;
            }
        }
    }
}


public static void main(String[] args) {
    try {
        int numberOfPoints = Integer.parseInt(args[0]);
        if (args.length == 1) {
            System.out.println("Calculando...");
            Points2DN puntos = new Points2DN(numberOfPoints);
            imprimePuntos(puntos.points);
            printClosest(puntos);
        }

        else {
            // Nothing
        }

    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("Not a valid number of points");
    } catch (Exception e) {
        System.out.println(e.toString());
    }

}

}

问题发生在下面片段的最后一行,,在尝试使用数组中的一个Point对象&#34; points&#34;之后。他们应该填充&#34; int&#34;在(x,y)上键入值(0,0),并进行初始化。我用尺寸检查了尺码:

 points.length

结果是我在IDE上设置为运行项目的参数。例如,如果&#39; n&#39;是10,那个值是10。这没关系,所以...为什么地狱不起作用!??

 Points2DN(int n) {
    this.points = new Point[n];
    for (int i = 0; i < n; i++) {
        this.points[i].x =  (int) Math.floor(Math.random()*100);

2 个答案:

答案 0 :(得分:5)

new Point[n]创建一个Point的数组,但它不会创建任何实际的Point个对象来放在该数组中。这就像购买一个可以存放书籍的盒子一样 - 除非你把书放在那里,否则它实际上不会有任何书籍。

您需要为要使用的每个索引元素创建Point,例如:

for (int i = 0; i < n; i++) {
    points[i] = new Point();
    points[i].x =  (int) Math.floor(Math.random()*100);
    ...

答案 1 :(得分:0)

按照yshavit的建议,并修改了一些代码,这就是结束的方式。

/*
 * Points2DN - Algorithms to calculate distance in 2D
 *
 * Author:
 * @Ruyman
 */

package com.exercises.algorithms;
import java.awt.*;
// import java.util.*;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class Points2DN {
    private Point[] points;
    protected int[] closestPoints = new int[2];
    private BigDecimal distance;

    Points2DN(int n) {
        this.points = new Point[n];
        for (int i = 0; i < n; i++) {
            this.points[i] = new Point();
            this.points[i].x =  (int) Math.floor(Math.random()*100);
            this.points[i].y =  (int) Math.floor(Math.random()*100);
        }
        this.distances(this);
    }

    Points2DN(int n, int x) {
        this.points = new Point[n];
        for (int i = 0; i < n; i++) {
            this.points[i] = new Point();
            this.points[i].x =  (int) Math.floor(Math.random()*100);
            this.points[i].y =  (int) Math.floor(Math.random()*100);
        }
        this.distances(this, x);
    }

    public static void imprimePuntos(Point[] arrayPuntos) {
        for (int i = 0; i < arrayPuntos.length; i++) {
            System.out.println("Point2D " +(i+1) + ": " + "( "
                    + arrayPuntos[i].x + " , "
                    + arrayPuntos[i].y + " )");
        }
    }

    public static void printClosest(Points2DN puntos) {
        Point p = puntos.points[puntos.closestPoints[0]];
        Point q = puntos.points[puntos.closestPoints[1]];
        System.out.println("\nThe closest points are: ");
        System.out.println("Points: "
                    + "(" + p.x + "," + p.y + ")" + " & "
                    + "(" + q.x + "," + q.y + ")");
        System.out.println("Distance: " + puntos.distance);
    }

    private void distances(Points2DN puntos) {
        for (int i = 0; i < puntos.points.length; i++) {
        // Iterative loop for the other points to be check with
            for (int j = (i+1); j < this.points.length; j++) {
                if (i == 0 && j == 1) {
                    int p = puntos.closestPoints[0] = i;
                    int q = puntos.closestPoints[1] = j;
                    puntos.distance = new BigDecimal((puntos.points[p]).distance(puntos.points[q])).setScale(3, RoundingMode.FLOOR);
                    continue;
                }
                BigDecimal distancePair = new BigDecimal(puntos.points[i].distance(puntos.points[j]));
                if (distancePair.doubleValue() < puntos.distance.doubleValue()){
                    puntos.closestPoints[0] = i;
                    puntos.closestPoints[1] = j;
                    puntos.distance = distancePair.setScale(3, RoundingMode.FLOOR);
                }
            }
        }
    }

    private void distances(Points2DN puntos, int x) {
        for (int i = 0; i < puntos.points.length; i++) {
            // Iterative loop for the other points to be check with
            for (int j = (i+1); j < this.points.length; j++) {
                if (i == 0 && j == 1) {
                    int p = puntos.closestPoints[0] = i;
                    int q = puntos.closestPoints[1] = j;
                    puntos.distance = new BigDecimal((puntos.points[p]).distance(puntos.points[q])).setScale(x, RoundingMode.FLOOR);
                    continue;
                }
                BigDecimal distancePair = new BigDecimal(puntos.points[i].distance(puntos.points[j]));
                if (distancePair.doubleValue() < puntos.distance.doubleValue()){
                    puntos.closestPoints[0] = i;
                    puntos.closestPoints[1] = j;
                    puntos.distance = distancePair.setScale(x, RoundingMode.FLOOR);
                }
            }
        }
    }

    /* main (String n, string x) {}
     * Generates 'n' random 2D points
     * Calculates which pair is the closest one
     * Print the result including the distance with 'x' decimals
     */ 

    public static void main(String[] args) {
        try {
            int numberOfPoints = Integer.parseInt(args[0]);
            if (args.length == 1) {
                Points2DN puntos = new Points2DN(numberOfPoints);
                imprimePuntos(puntos.points);
                printClosest(puntos);
            }

            if (args.length == 2) {
                int x = Integer.parseInt(args[1]);
                Points2DN puntos = new Points2DN(numberOfPoints, x);
                imprimePuntos(puntos.points);
                printClosest(puntos);
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }
}