java.lang.NullPointerException [英] java.lang.NullPointerException

查看:100
本文介绍了java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在获取距离变量java.lang.NullPointerException,我不明白为什么.有人可以建议我吗?

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

推荐答案

java.lang.NullPointerException
这只是意味着您正在尝试使用实际上是NULL的对象的属性

在引发此异常的地方进行调试,然后检查是否有任何对象为NULL并尝试使用其属性.


仅用于您的知识库和参考 [调试java.lang.NullPointerException的建议 [
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[^]


如果要发布StackTrace和行号查找错误会容易得多...此外,您可以将从Math.Min等获得的温度值分配给局部变量,这样stacktrace可能会更加准确...

干杯,阿恩特
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


这篇关于java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆