import java.util.*;
 class Point {
double x,y;
Point(double a, double b){
x = a;
y = b;
}
 double s = Math.sqrt(x*x+y*y); }    public class Pointshow{
     public static void main(String[] args) {
   Point d[]=new Point[10];
   for(int i=0;i<10;i++){
  d[i].x=Math.random();
      d[i].y=Math.random();
   }
   Arrays.sort(d);
   for(int i=0;i<10;i++){
      System.out.println("点的坐标是:("+d[i].x+","+d[i].y+")"+"点到原点的距离是:"+d[i].s);
   }  
// TODO Auto-generated method stub }
}

解决方案 »

  1.   

    为什么程序运行总会报这个错呢?Exception in thread "main" java.lang.NullPointerException
    at Pointshow.main(Pointshow.java:16)
      

  2.   

    我认为这一句是错误的,你怎么能用Arrays.sort(d)为d排序呢,你排序的原则是什么,必须使point类实现comparator接口,重新compare方法,
    然后调用  Arrays.sort(d,comparator);
      

  3.   


    import java.util.*;
     class Point {
        double x,y;
        Point(double a, double b){
            x = a;
            y = b;
        }
        public double getDistance() // 这里最好用方法,别直接计算,当然你直接算也是可以的
        {
         return  Math.sqrt(x*x+y*y);
        } }
     
    class myCoparator implements Comparator //实现Comparator接口
     {
        public int compare(Object x1,Object x2)
         {
            Point pb1=(Point)x1;
            Point pb2=(Point)x2;
           
            double d1=pb1.getDistance();
            double d2=pb2.getDistance();
            if( d1>d2) return 1;
            if(d1==d2) return 0;
            return -1;
         }
     }
        public class Pointshow{
             public static void main(String[] args) {
               Point d[]=new Point[10];
               for( int i=0;i<10;i++)
               {
                d[i]=new Point(Math.random(),Math.random());   //这是你显示空指针的原因
               }
               myCoparator mc=new myCoparator();
               
               Arrays.sort(d,mc);
               for(int i=0;i<10;i++){
                  System.out.println("点的坐标是:("+d[i].x+","+d[i].y+")"+"点到原点的距离是:"+d[i].getDistance());
               }  
            // TODO Auto-generated method stub    }
    }
      

  4.   

    设置个断点在for循环调用构造方法赋值试试
     for(int i=0;i<10;i++)
      point[i]=new Point(Math.random(),Math.random());
      

  5.   

    为什么程序运行总会报这个错呢?Exception in thread "main" java.lang.NullPointerException 
    at Pointshow.main(Pointshow.java:16)
    是因为d[i]没有new
    for(int i=0;i<10;i++){
                  d[i] = new Point(Math.random(), Math.random());
                  //d[i].x=Math.random();
                  //d[i].y=Math.random();
               }Arrays.sort(d);//这里是有问题的,LS的说了,奇怪的是LZ能编译通过?
      

  6.   

    import java.util.*;
     class PointXY implements Comparable<PointXY>
     {    
       public PointXY(double x, double y)
        {
            this.x = x;
            this.y = y;        
        }
        public int compareTo(PointXY other)
        {
          if(distance<other.distance)
          return -1;
          if(distance>other.distance)
          return 1;
          return 0;
        }
        public double getDistance()
        {
           distance = Math.sqrt(x*x+y*y);
           return distance;
        }
         double x,y,distance;
     }
        public class PointShow 
        {
             public static void main(String[] args) 
             { 
               PointXY[] d=new PointXY[10];
               double[] dis=new double[10];
               for(int i=0;i<10;i++)
               {
                  double xx=Math.random();
                  double yy=Math.random();
                  d[i]=new PointXY(xx,yy);
                  dis[i]=d[i].getDistance();
               }
               Arrays.sort(dis);
               for(int i=0;i<10;i++)
               System.out.println(dis[i]);
             } 
        }
    初学Java一个月,point类还没学,不过那个错误应该是你用 了point的原因,应该改一下类名
      

  7.   

    注意区分Point d[]=new Point[10];和d[i] = new Point(Math.random(),Math.random());的区别,前者创建的是数组,而后者创建的才是实际的对象。而且我觉得你的距离计算过程和构造方法的顺序还有问题哦