0

I'm having a problem with my code. Displays the message: java.lang.NullPointerException: Attempt to invoke the interface method 'int java.util.List.size ()' on a null object reference. My code is:` public class MainActivity extends FragmentActivity implements GoogleMap.OnMyLocationButtonClickListener, OnMyLocationClickListener, OnMapReadyCallback {

private GoogleMap maps;
ArrayList<LatLng> markerPoints=new ArrayList<>();
MarkerOptions markerOptions;
MarkerOptions markerOpt;
LatLng latLng;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //parapobh sto SupportMapFragment ths Activity_main
    SupportMapFragment fm = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));

    /* lhpsh xarth gia to SupportMapFragment */
    fm.getMapAsync(this);

}

@Override
public void onMapReady(GoogleMap googleMap) {
    maps = googleMap;
    LatLng Greece = new LatLng(37.97, 23.73);
    //googleMap.addMarker(new MarkerOptions().position(Greece).title("Marker in Greece"));
    //googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Greece,16));
    Button btn_find = (Button) findViewById(R.id.btn_find);
    OnClickListener findClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText etLocation = (EditText) findViewById(R.id.et_location);
            String location = etLocation.getText().toString();

            if (location != null && !location.equals("")) {
                new GeocoderTask().execute(location);
            }
        }
    };
    btn_find.setOnClickListener(findClickListener);
    Button btn_go=(Button) findViewById(R.id.btn_go);
    OnClickListener goClickListener=new OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText etDieuthinsh=(EditText) findViewById(R.id.et_dieuthinsh);
            String dieuthinsh=etDieuthinsh.getText().toString();
            if(dieuthinsh!=null && !dieuthinsh.equals("")){
                new GeocoderTask1().execute(dieuthinsh);
            }
        }
    };
    btn_go.setOnClickListener(goClickListener);
}
private class GeocoderTask extends AsyncTask<String,Void,List<Address>> {
    @Override
    protected List<Address> doInBackground(String... locationName) {
        Geocoder geocoder = new Geocoder(getBaseContext());
        List<Address>addresses=null;
            try {
                addresses = geocoder.getFromLocationName(locationName[0], 8);
            } catch (IOException e) {
                e.printStackTrace();
            }

        return addresses;
    }

    @Override
    protected void onPostExecute(List<Address>addresses) {

       if (addresses==null||addresses.size()==0) {
         Toast.makeText(getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
        }

       maps.clear();

        //try {
            for (int t = 0; t < addresses.size(); t++) {
                Address address = (Address) addresses.get(t);
                latLng = new LatLng(address.getLatitude(), address.getLongitude());

                String addressText = String.format("%s,%s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", address.getCountryName());

                markerOptions = new MarkerOptions();
                markerOptions.position(latLng);

                /**
                 * for the start location,the color of marker is Green and*
                 * for the end location, the color of marker is Red
                 */

                markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));

                //prosthesh neou deikth sto GoogleMaps Android API V2
                markerOptions.title(addressText);
                maps.addMarker(markerOptions);

                if (t == 0)
                    maps.animateCamera(CameraUpdateFactory.newLatLng(latLng));

                if (markerPoints.size() == 1) {
                    //lhpsh URL sto Google Directions API
                    String url = getDirectionsUrls(markerPoints.get(0));

                    DownloadTask downloadTask = new DownloadTask();

                    //xekinhste thn lhpsh dedomenwn json apo to Google Directions API
                    downloadTask.execute(url);
                }
            }
        //} catch(NullPointerException e){
        //   e.printStackTrace();
       // }
        }
}
         private class GeocoderTask1 extends AsyncTask<String, Void, List<Address>> {
             @Override
             protected List<Address> doInBackground(String... dieuthinshName) {
                 Geocoder geocoders = new Geocoder(getBaseContext());
                 List<Address> dieuts = null;
                 try {
                  dieuts = geocoders.getFromLocationName(dieuthinshName[0], 8);
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
                 return dieuts;
             }

             @Override
             protected void onPostExecute(List<Address> dieuts) {
                if (dieuts == null || dieuts.size() == 0) {
                    Toast.makeText(getBaseContext(), "No Dieuthinsh found", Toast.LENGTH_SHORT).show();
                }
                 maps.clear();

                 //try{
                for (int k = 0; k < dieuts.size(); k++) {
                     Address address1 = (Address) dieuts.get(k);
                     latLng = new LatLng(address1.getLatitude(), address1.getLongitude());
                     String address1Text = String.format("%s,%s", address1.getMaxAddressLineIndex() > 0 ? address1.getAddressLine(0) : "", address1.getCountryName());

                     markerOpt = new MarkerOptions();
                    markerOpt.position(latLng);

                markerOpt.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));

                     markerOpt.title(address1Text);
                    maps.addMarker(markerOpt);

                    if (k == 0)
                        maps.animateCamera(CameraUpdateFactory.newLatLng(latLng));

                        if(markerPoints.size()==2) {

                            //lhpsh URL sto Google Directions API
                            String url1 = getDirectionsUrl(markerPoints.get(1));

                            DownloadTask1 downloadTask1 = new DownloadTask1();

                            //xekinhste thn lhpsh dedomenwn json apo to Google Directions API
                            downloadTask1.execute(url1);
                        }
                 }
                // } catch (NullPointerException e) {
                  //   e.printStackTrace();
                 //}
             }
        }

the answers that exist on the site do not solve the problem.

L.Areti
  • 1
  • 1
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Vidhi Dave Jun 14 '18 at 11:22

1 Answers1

2

Instead of declaring List<Address>addresses=null; do this

List<Address>addresses = new ArrayList<>();

the same with List<Address> dieuts = null;

List<Address> dieuts = new ArrayList<>();

this should solve the problem

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
  • Thank you very much for the answer and the time you spent to see the problem in my code – L.Areti Jun 14 '18 at 13:20
  • @L.Areti Just to clarify, the "accepted answer" should be the one that helped you solve your problem, not just as a way of saying "thanks for looking". Accepted answers act as a guide to those who may find this question in the future. Did a reboot of your device solve the issue by itself, or did Gaston's advice to initlalise your lists stop the crash? – Michael Dodd Jun 14 '18 at 13:44
  • 1
    yes, thats why im confused, since my answer maybe is the right one , because op is initializing the list with null and maybe the List.size() is returning null because of it – Gastón Saillén Jun 14 '18 at 13:46