【发布时间】:2013-11-07 19:40:38
【问题描述】:
public class CountryLookupTest {
public static void main(String[] args) {
String location1=null;
try {
int number=0;
LookupService citylookup = newLookupService("D://GeoLiteCity.dat",
LookupService.GEOIP_MEMORY_CACHE );
FileReader fr =new FileReader("d:\\IP.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while( (line = br.readLine()) != null ){
location1=line;
System.out.println(location1);
Location record = citylookup.getLocation(location1);
Site S[]= new Site[100];
S[number]= new Site(record.longitude,record.latitude);
number++;
System.out.println("longtitude " + S[0].getLongtitude());
System.out.println("latitude " + S[0].getLatitude());
}
public class Site {
private float longitude;
private float latitude;
public Site(float a, float b){
setLongitude(a);
setLatitude(b);
}
}
我使用我的主类读取逐行保存IP地址的txt,并希望将它们保存到对象中并保存到数组中。
我测试了我的代码,我得到了运行时错误。
140.118.175.208
经度 121.524994
纬度 25.0392
线程“主”java.lang.NullPointerException 中的异常
我添加 S[1] System.out.println("longtitude" + S[1].getLongtitude());
它向我展示了同样的问题并且不打印 S[1] 值
我不知道发生了什么?我想我已经分配了数组 obj?谢谢!
【问题讨论】:
-
你能检查记录是否为空吗?
-
NullPointerException 发生在哪一行?堆栈跟踪将为您的课程提供一个行号。
-
我的回答解决了您的问题吗?
-
是的,将其移出循环。谢谢。
标签: java nullpointerexception maxmind