compareTo方法出错

时间:2014-04-07 00:43:52

标签: java nullpointerexception comparable

我的邮局文件中的一切运行良好,除非我运行邮局文件,它说我的compareTo方法存在问题,这是在我的信件文件中。错误如下:

 ----jGRASP exec: java PostOffice
Exception in thread "main" java.lang.NullPointerException
    at Letter.compareTo(Letter.java:33)
    at Letter.compareTo(Letter.java:1)
    at SortSearchUtil.selectionSort(SortSearchUtil.java:106)
    at PostOffice.sortLetters(PostOffice.java:73)
    at PostOffice.main(PostOffice.java:15)

 ----jGRASP wedge: exit code for process is 1.
 ----jGRASP: operation complete.

我不知道我的方法有什么问题。我的compareTo方法应该将当前的信件与首先通过邮政编码传递的信件进行比较,然后根据邮政编码是相同的地址的街道值进行比较。

这是我的邮局方法:

import java.util.*;
import java.io.*;

public class PostOffice 
{

   private final int max = 1000;
   private Letter [] ltrAra = new Letter[max];
   private int count;

   public static void main(String [] args) 
   {
      PostOffice postOffice = new PostOffice();
      postOffice.readLetters("letters.in");
      postOffice.sortLetters();
      postOffice.printLetters();
   }

   public PostOffice() 
   {
      Letter [] Letters = ltrAra;
      this.count = 0;
   }

   public void readLetters(String filename) 
   {
      int count = 0;
      int iWork = 0;

      Scanner fin = new Scanner(filename);

      String toName, toStreet, toCity, toState, toZip;
      String fromName, fromStreet, fromCity, fromState, fromZip, temp;
      double weight;
      String sWork;
      fin = FileUtil.openInputFile(filename);
      if (fin != null)
      {
         while (fin.hasNext())
         {
            toName = fin.nextLine();
            toStreet = fin.nextLine();
            sWork = fin.nextLine();
            iWork = sWork.indexOf(",");
            toCity = sWork.substring(0, iWork);
            iWork = iWork + 2;
            toState = sWork.substring(iWork, iWork + 2);
            iWork = iWork + 3;
            toZip = sWork.substring(iWork);

            fromName = fin.nextLine();
            fromStreet = fin.nextLine();
            sWork = fin.nextLine();
            iWork = sWork.indexOf(",");
            fromCity = sWork.substring(0, iWork);
            iWork = iWork + 2;
            fromState = sWork.substring(iWork, iWork + 2);
            iWork = iWork + 3;
            fromZip = sWork.substring(iWork);

            sWork = fin.nextLine();
            weight = Double.parseDouble(sWork);   

            ltrAra[count] = new Letter(toName, toStreet, toCity, toState, toZip, fromName, fromStreet, fromCity, fromState, fromZip, weight);      
            count++;
            }
            fin.close();
         }
   }

   public void sortLetters() 
   {
     SortSearchUtil.selectionSort(ltrAra);
   }

   public void printLetters() 
   {
      for (Letter ltr : ltrAra)
      {
         System.out.println(ltr);
         System.out.println();
      }
   }
}

这是我的信件方法:

public class Letter extends PostOffice implements Comparable<Letter> 
{
   private static final double postageRate = 0.46;
   private String fromName;
   private Address fromAddress;
   private String toName;
   private Address toAddress;
   private double weight;


   public Letter (String fromName, String fromStreet, String fromCity, String fromState, String fromZip, String toName, String toStreet, String toCity, String toState, String toZip, double weight) 
   {
      this.fromName = fromName;
      this.fromAddress = new Address(fromStreet, fromCity, fromState, fromZip);
      this.toName = toName;
      this.toAddress = new Address(toStreet, toCity, toState, toZip);

      this.weight = weight;   
   }

   public String toString() 
   {
      String result;
      result = String.format("from: %s\t\t\t%5.2f\n%s", fromName, Letter.getPostage(weight), fromAddress);
      result = result + String.format("\n\n\t\t\tTo: %s\n\t\t\t%s", toName, toAddress);

      return result;
   }

   public int compareTo(Letter that) 
   {
      int value;
      value = this.toAddress.getZip().compareTo(that.toAddress.getZip());
      return value;
   }


   public static double getPostage(double weight) {
      double workWeight;
      workWeight = weight + 0.999;
      workWeight = (int)workWeight;   
      return workWeight * postageRate;
   } 
}

3 个答案:

答案 0 :(得分:0)

只要that为空,您就会收到异常,而且您的变量赋值也是多余的。

public int compareTo(Letter that) 
{
  if (this.toAddress == null) {
    if (that == null || that.toAddress == null) {
      return 0;
    }
    return -1;
  }
  if (that == null) return 1; // <-- Add something like this.
  // int value;
  // value = this.toAddress.getZip().compareTo(that.toAddress.getZip());
  return this.toAddress.getZip().compareTo(that.toAddress.getZip());
}

答案 1 :(得分:0)

在compareTo方法中

,您不检查“that”参数是否为空。

你应该尝试类似的东西:

public int compareTo(Letter that){   
  if(that == null) return 1;
  int value;
  value = this.toAddress.getZip().compareTo(that.toAddress.getZip());
  return value;
} 

答案 2 :(得分:0)

你的问题是ltrAra有1000个条目,但没有那么多的信件。因此,当您排序时,您正在尝试对某些空值进行排序。但是,当您将其中一个真实对象与其中一个空值进行比较时,您的比较会抛出异常。

当参数为null时,您的比较需要以不同的方式运行。例如,你可以使它总是在“真实”值之后对null进行排序。