java.lang.NullPointerException:尝试调用接口方法' java.lang.Object java.util.List.get(int)'在null对象引用上

时间:2017-12-08 07:09:41

标签: java android nullpointerexception

geocoder = new Geocoder(MapsActivity.this);
                List<Address> addressList = null;

               try {
                    addressList = geocoder.getFromLocation(firstLocationLattiutde, firstLocationlongitude, 5);
                }
                catch (IOException e) {
                    e.printStackTrace();
                }

                Address obj = addressList.get(0);
                add = obj.getAddressLine(0);
                guardAddress.setText(add);

但有时会出现这样的错误

  

java.lang.NullPointerException:尝试调用接口方法&#39; java.lang.Object java.util.List.get(int)&#39;在com.example.fahadaziz.securityapplication.Services.MyService.onLocationChanged

上的空对象引用

1 个答案:

答案 0 :(得分:0)

geocoder.getFromLocation()方法正在返回null。 因此,在对addresslist执行任何操作之前,您必须检查null

if(addresslist != null)
{
    //go ahead
}