0

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.

3
  • 2
    Either city or other are null. Use a debugger. Commented Apr 2, 2014 at 15:22
  • Concerning //Did I do this correctly?, it depends on what you want to do. Do you want to compare Strings? Commented Apr 2, 2014 at 15:29
  • I'm attempting to compare the city names. How do I make other not NULL? Commented Apr 2, 2014 at 15:54

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Could I make a clone of my array, and then delete all the NULL's that are in it? I have to use regular Array in this program sadly.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.