我正在尝试编写一个处理地点位置的课程,例如......
00210 Portsmouth NH 43.005895 -71.013202
00211 Portsmouth NH 43.005895 -71.013202
我的班级......
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?
}
}
我的主要人物:
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());
}
}
我尝试将Comparable添加进去,然后尝试对codesArray
进行排序,一切正常。我得到......
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)
作为一个错误。我不确定我在这里得到一个NULL指针异常,是因为我的codesArray
未满,或者我在哪里出错了。感谢任何和所有的帮助,我真的很感激。
答案 0 :(得分:1)
如果输入中的行少于50000行,则数组中始终存在空元素。而PostalCodes.compareTo(null)会抛出NPE。
我建议使用ArrayList&lt; PostalCode&gt;和Collections.sort而不是数组和Arrays.sort
答案 1 :(得分:0)
我能看到的最简单的解决方案是在比较之前检查PostalCode是否为空。
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());
}
但是,如果列表中的某些PostalCodes为null,则循环中的打印行也将失败,因为您无法在空对象上调用toString()方法。
for (int i =0;i<counter; i+=1000)
{
if(codesArray[i] == null)
{
continue;
}
System.out.println(codesArray[i].toString());
}