0
        public boolean onQueryTextSubmit(String query) {
            String location = searchView.getQuery().toString();
            List<Address> addressList = null;

            if (location != null || !location.equals("")){
                Geocoder geocoder = new Geocoder(Home.this);
                try {
                    addressList = geocoder.getFromLocationName(location,1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Address address = addressList.get(0);
                LatLng latLng = new LatLng(address.getLatitude(),address.getLongitude());
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 12));
            }

Why I get NullPointerException error from line Address address = addressList.get(0). I try to create searchbar in maps activity.

iknow
  • 8,358
  • 12
  • 41
  • 68
Kimppi
  • 1

1 Answers1

0

You are trying to access a method on null. Try initializing addresslist with an empty list or check first if addresslist is not null, before accessing any methods.

List<Address> addressList = new ArrayList<Address>;

See: How to make a new List in Java and What is a NullPointerException, and how do I fix it?

Tom
  • 4,070
  • 4
  • 22
  • 50