java - Error with compareTo method -


everything runs in post office file except when run post office file, says there problem compareto method in letters file. error reads:

 ----jgrasp exec: java postoffice exception in thread "main" java.lang.nullpointerexception     @ letter.compareto(letter.java:33)     @ letter.compareto(letter.java:1)     @ sortsearchutil.selectionsort(sortsearchutil.java:106)     @ postoffice.sortletters(postoffice.java:73)     @ postoffice.main(postoffice.java:15)   ----jgrasp wedge: exit code process 1.  ----jgrasp: operation complete. 

i don't know wrong method. compareto method supposed compare current letter 1 passed in first zip code, street value of address if zip codes same.

here's 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()     {       (letter ltr : ltrara)       {          system.out.println(ltr);          system.out.println();       }    } } 

here's 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;    }  } 

you exception whenever that null, variable assignment 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 this.   // int value;   // value = this.toaddress.getzip().compareto(that.toaddress.getzip());   return this.toaddress.getzip().compareto(that.toaddress.getzip()); } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -