Jump to content

java.lang.NullPointerException

- - - - -

  • Please log in to reply
4 replies to this topic

#1
PatrickM

PatrickM

    Newbie

  • Members
  • Pip
  • 8 posts
I'm geting java.lang.NullPointerException for the distance variable



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[/B]

 

 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));[B]->here[/B]

       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]));[B]->here[/B]

     }

      }


     return distance;


     }


 }
package closestPair;


public class Tests {

	public static void main(String [ ] args) throws InvalidNumberOfTestsException

	{

		ClosestPair.getRuntimes(10, 20, "f");

	}


}
• public static void getRuntimes(int p, int t, String f)
does the following:
(a) Generates p sets of points for various sizes starting from 10 (the sets are not random so
that experiments are repeatable).
(b) Finds a closest pair of points using naiveClosestPair and closestPair, and
takes the cpu times for these.
© Records the worst case times for each algorithm implementation taken over the p sets
(note that it is pefectly possible that the worst case runtimes for the two algorithm implementations
occur for different sets).
(d) Repeats the above with sets of size 20, 30, . . . , 10t.
(e) Outputs the result for each iteration on the file named f in the format
input-set-size naiveClosestPair-worst-case-runtime
ClosestPair-worst-case-runtime
What should I do?Thanks in advance

Attached Files


Edited by PatrickM, 28 February 2011 - 12:17 AM.


#2
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
I pasted your code in eclipse, but I'm getting an error (obviously because I'm missing the rest of your classes). Can you tell me which line was giving you the NullPointerException or past he first few line of the exception? I don't think distance is giving you the exception since it's an int.


#3
PatrickM

PatrickM

    Newbie

  • Members
  • Pip
  • 8 posts
@sourlemon...I"ve uploaded the files...I'm getting the exceptions on the lines where distance is...

#4
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
I'm not sure 100% sure if this is the problem, but this doesn't look right to me.

 for (int i = 0; i <Y.length; i++)
    {
          int Z = Y[i].getY();
          if ((W-Z)*(W-Z) <= distance)
      {
          [COLOR=red]shortDist[n] = Y[i];[/COLOR] //Did you to mean to always write to shortDist[0]?

           }
        }

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

     return distance;

     }

You shouldn't use shortDist.length to end the loop because you set the length to Y.length. However, that's not true because you only add to shortDist if ((W-Z)*(W-Z) <= distance). So you have to find out what the true length of shortDist is.

Edited by sourlemon, 04 March 2011 - 10:30 PM.



#5
hArrayList

hArrayList

    Newbie

  • Members
  • Pip
  • 7 posts
Should the Point[] arrays not be declared some heap space first. I.e Point[] X = new Point();. This exception usually means something has not been given a reference. Check through your declarations.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users