15,964,188 members
See more:
I'm geting java.lang.NullPointerException for the distance variable and i dont understand why.Can someone please advice me?

```package closestPair;

import java.util.*;

/** The  implementation of Shamos's Algorithm. */
public class Code {

/** Find the square distance of the closest pairs in the point set. This static function is the preparation step for the recursive part of the algorithm defined in the method closestPairAux. */
public static int closestPair(PointSet P)
throws TrivialClosestPairException, UnknownSortOptionException
{
int distance = 0;//
Point[] X = P.sort('x');
Point[] Y = P.sort('y');

distance = closestPairAux(X, Y);[B]->here

return distance;

}

/** The recursive part of Shamos's Algorithm. The parameter X is an array of points sorted by the X axis and the parameter Y is the same array of points sorted by the Y axis. The burden of work is going on here. Good luck! */
public static int closestPairAux(Point[] X, Point[] Y)
throws TrivialClosestPairException, UnknownSortOptionException
{//trivial case
if (X.length<4){
return PointSet.naiveClosestPair(new PointSet (X));

}
//get the middle element of the array here
int centerIndex = X.length/2;
int W = X[centerIndex].getX();

//the two disjoint sets devided by the conceptual sweep line
Point[] PL = Arrays.copyOfRange(X,(int) 0,(int) Math.ceil(centerIndex));
Point[] PR = Arrays.copyOfRange(X,(int) Math.floor(centerIndex), (int) X.length-1);
//the conquering part,taking the min from the two sets
int distance = Math.min(closestPairAux(PL,Y),closestPairAux(PR,Y));->here
Point[] shortDist = new Point[Y.length];
//part 4.b of the shamos algorithm
int n = 0;
for (int i = 0; i <Y.length; i++)
{
int Z = Y[i].getY();
if ((W-Z)*(W-Z) <= distance)
{
shortDist[n] = Y[i];

}
}
//part 4.c of the shamos algorithm
// collecting those elements which are near the divider in the shortDist-array.
for (int i =0; i< shortDist.length -1; i++)
{
for (int j = i+1; j <shortDist.length-1 && j< i + 7; j ++){
distance = Math.min(distance, shortDist[i].sqrDist(shortDist[j]));->here
}
}

return distance;

}

}```

```package closestPair;

public class Tests {
public static void main(String [ ] args) throws InvalidNumberOfTestsException
{
ClosestPair.closestPairCheck(10, 400);
}

}```

What should I do?Thanks in advance
Posted

## Solution 1

java.lang.NullPointerException
This simply means that you are trying to use a property of an object which is actually a NULL

DEBUG around the lines from where this exception is thrown and then check if any object is NULL whose property you are trying to use.

Just for your knowledgebase and reference[^].

This also is a good one: Suggestions for debugging java.lang.NullPointerException[^]

AkilMai 27-Feb-11 14:33pm
I know that...but I don't know how to fix it since what I've written looks fine(but apparently it isn't)
Espen Harlinn 28-Feb-11 11:55am
Good effort - "DEBUG around the lines ..." would certainly help, my 5

## Solution 2

If you would post the StackTrace and linenumbers it would be much easyer to look for an error... Additionally, you can assing the temp-values you get from Math.Min etc. to local variables, so the stacktrace might be even more accurate...

Cheers, Arndt