Question

Everything runs well in my post office file except when I run the post office file, it says there is a problem with my compareTo method which is in my letters file. The error reads:

 ----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.

I don't know what could be wrong with my method. My compareTo method is supposed to compare the current Letter to the one passed in first by zip code, then by street value of the to address if the zip codes are the same.

Here's my Post Office Method:

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();
      }
   }
}

Here's my letters method:

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;
   } 
}
Was it helpful?

Solution

You will get an exception whenever that is null, also your variable assignment is redundant.

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());
}

OTHER TIPS

in your compareTo method you are not checking if the "that" parameter is null.

you should try something like:

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

Your problem is that ltrAra has 1000 entries, but there aren't as many letters as that. So when you sort, you're trying to sort some nulls. But when you compare one of the real objects to one of the nulls, your comparison throws the exception.

Your comparison needs to behave differently when its argument is null. You could, for example, make it so that null is always sorted after the "real" values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top