简体   繁体   中英

Error while sorting an Array of Objects

I'm attempting to write a class that deals with locations of places such as...

00210   Portsmouth  NH  43.005895   -71.013202
00211   Portsmouth  NH  43.005895   -71.013202

My class...

public class PostalCodes implements Comparable<PostalCodes> {
private String city;
private double latitude;
private double longitude;
private String zip;
private String state;

public PostalCodes(String aZip, String aCity, String aState, double aLatitude, double aLongitude)
{
    city = aCity;
    latitude = aLatitude;
    longitude = aLongitude;
    zip = aZip;
    state = aState;
}

void setZip(String aZip)
{
    zip=aZip;
}

void setState(String aState)
{
    state=aState;
}


void setLocation(String aCity)
{
    city = aCity.trim();
}
void setLatitude(double lat)
{
    latitude = lat;
}
void setLongitude(double long1)
{
    longitude = long1;
}
public String getState()
{
    return state;
}
public String getZip()
{
    return zip;
}
public String getLocation()
{
    return city;
}
public double getLatitude()
{
    return latitude;
}
public double getLongitude()
{
return longitude;
}
public String toString()
{
    String result = String.format("%s %s,%s (%1.3f; %1.3f)",zip, city, state, latitude,longitude);
    return result;
}


@Override
public int compareTo(PostalCodes other) {
    return city.compareTo(other.getLocation()); //Did I do this correctly?
}

}

My main:

public class Hmwk {

public static void main(String[] args) throws IOException {
    URL url = new URL("http://noynaert.net/zipcodes.txt");
    Scanner input=new Scanner (url.openStream());
    int counter =0;
    final int MAX_SIZE=50000;
    PostalCodes[] codesArray= new PostalCodes[50000];
    while (input.hasNextLine() && counter < MAX_SIZE)
    {
        String line=input.nextLine();
        String[] tokens;
        tokens = line.split("\t");
        if (tokens.length != 5)
        {
            continue;
        }
        String zip=tokens[0];
        String city=tokens[1];
        String state=tokens[2];
        double lat=Double.parseDouble(tokens[3]);
        double longy=Double.parseDouble(tokens[4]);
        PostalCodes code=new PostalCodes(zip,city,state,lat,longy);
        codesArray[counter]= code;
        counter++;

    }
    Arrays.sort(codesArray); //Error here
    for (int i =0;i<counter; i+=1000)
    {
        System.out.println(codesArray[i].toString());
    }

}

I had everything working right until I tried to add the Comparable in, and then tried to sort codesArray . I'm getting...

Exception in thread "main" java.lang.NullPointerException
at PostalCodes.compareTo(PostalCodes.java:69)
at PostalCodes.compareTo(PostalCodes.java:1)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at Hmwk.main(Hmwk.java:37)

As an error. I'm not sure how i'm getting a NULL pointer exception here, is it because my codesArray isn't full, or where else am I going wrong here. Thanks for any and all help, I really appreciate it.

if there are less than 50000 lines in your input there always be null elements in your array. And PostalCodes.compareTo(null) throws NPE.

I suggest to use ArrayList<PostalCode> and Collections.sort instead of an array and Arrays.sort

The simpliest solution I can see is check if the PostalCode is null before the comparaison.

public int compareTo(object obj)
{
  this.compareTo((PostalCodes)obj);
}

public int compareTo(PostalCodes other)
{
  if(other == null)
  {
    return 1;
  }

  if(city == null)
  {
    return 0;
  }

  return city.compareTo(other.getLocation());
}

However, if some of your PostalCodes in the list are null, the print line in your loop will also fail because you can't call the toString() method on a null object.

for (int i =0;i<counter; i+=1000)
{
    if(codesArray[i] == null)
    {
      continue;
    }
    System.out.println(codesArray[i].toString());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM