javaarraysnetbeans-7.4

Point[] array throws NullPointerException when trying to set variables


Well. I have tried EVERYTHING, and i've been looking for answers all over internet, including this website, but... No luck at all.

Here is my code:

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

}

}

The problem happens in the last line of the fragment below, right after trying to use one of the Point objects inside the array "points". They should be filled with "int" type values (0,0) on (x,y), and be initialized. I have checked the size with:

 points.length

And the outcome is the number i set as argument on my IDE to run the project. For example, if 'n' is 10, that value is ofc 10. It's ok, so... WHY THE HELL isn't this working!??

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

Solution

  • new Point[n] creates an array of Points, but it doesn't create any actual Point objects to sit in that array. It's like buying a box that can store books -- it won't actually have any books in it until you put them there.

    You need to create a Point for each index element you want to use, something like:

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