-1

MapActivity

public class Agriculture extends AppCompatActivity implements    
       OnMapReadyCallback, 
       GoogleMap.OnInfoWindowClickListener, 
       GoogleMap.OnMarkerClickListener {

    GoogleMap mMap;
    Address address;
    LatLng latLng;
    SupportMapFragment sMapFragment;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }


public void onSearch(View view)


 {

        EditText tfLocation = (EditText) findViewById(R.id.tfLocation);

        String location = tfLocation.getText().toString();

        List<Address> addressList = null;

        if (location!=null && !location.equals(""))
        {
            Geocoder geocoder = new Geocoder(this);
            try {

                addressList = geocoder.getFromLocationName(location, 0);
            } catch (IOException e) {
                e.printStackTrace();
            }

            Address address = addressList.get(0);
            LatLng latLng = new LatLng(address.getLatitude() , address.getLongitude());

logcat

Process: amahlo.gridview, PID: 13000
java.lang.IllegalStateException: Could not execute method for android:onClick
 at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
 at android.view.View.performClick(View.java:5210)
 at android.view.View$PerformClick.run(View.java:20976)
 at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:145)
 at android.app.ActivityThread.main(ActivityThread.java:6145)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
 at android.view.View.performClick(View.java:5210) 
 at android.view.View$PerformClick.run(View.java:20976) 
 at android.os.Handler.handleCallback(Handler.java:739) 
 at android.os.Handler.dispatchMessage(Handler.java:95) 
 at android.os.Looper.loop(Looper.java:145) 
 at android.app.ActivityThread.main(ActivityThread.java:6145) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:372) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
 at amahlo.gridview.Agriculture.onSearch(Agriculture.java:45)
 at java.lang.reflect.Method.invoke(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:372) 
 at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
 at android.view.View.performClick(View.java:5210) 
 at android.view.View$PerformClick.run(View.java:20976) 
 at android.os.Handler.handleCallback(Handler.java:739) 
 at android.os.Handler.dispatchMessage(Handler.java:95) 
 at android.os.Looper.loop(Looper.java:145) 
 at android.app.ActivityThread.main(ActivityThread.java:6145) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:372) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Error is on line number 45 of `Agriculture.java`. May be your `addressList` is null. `getFromLocationName()` will returns null or empty list if no matches were found or there is no backend service available. – Priyank Patel Sep 29 '16 at 10:25
  • provide your full layout & Activity code plz – Mohit Trivedi Sep 29 '16 at 10:30
  • provide full code b'z you have error in onclick so there may be some thing you missing. so we can point out that. – Wasim K. Memon Sep 29 '16 at 11:15
  • hi all thank you for your time, i have editted and added the layout and full activity @priyankPatel i have seen that the problem is there but cannot seem to fix it please help. – Stanley Tutu Dinala Oct 03 '16 at 09:36
  • The NullPointer is a side-effect of the other exception being thrown. Is that the entire logcat? – OneCricketeer Oct 03 '16 at 09:41
  • can you pl check addressList is not null and its size greater than 0 before getting the zeroth element. – Rajen Raiyarela Oct 03 '16 at 09:45
  • @RajenRaiyarela The list variable is explicitly set to null outside the try-catch. Adding an if-statement shouldn't be a solution. A better recommendation would be moving the `addressList.get(0);` into the `try` – OneCricketeer Oct 03 '16 at 09:47
  • i m not asking to set it to null. i m asking after calling getFromLocationName to check addressList is not null and size is greater than zero. i too have face this kind of NPE long time back when i did not receive any result for the geocoding. – Rajen Raiyarela Oct 03 '16 at 09:51
  • 1
    @cricket_007 thank you very much i have put the addressList.get(0); into the try it works you are a Star – Stanley Tutu Dinala Oct 03 '16 at 10:46
  • I mean, it "prevents" the error. I'm not sure if it completely solves your problem, though – OneCricketeer Oct 03 '16 at 10:48
  • it solves my problem and no error comes up i just need to set the map to zoom into the location searched on the map – Stanley Tutu Dinala Oct 03 '16 at 10:50
  • ok the error now comes when i press the search button when the Edit text is empty – Stanley Tutu Dinala Oct 03 '16 at 12:41

1 Answers1

0

Ignoring the fact that location can't be null...

You first set the list to null.

List<Address> addressList = null;

Then continue on to check if the string is empty and do some try-catching.

if (!TextUtils.isEmpty(location))
{
    Geocoder geocoder = new Geocoder(this);
    try {
        addressList = geocoder.getFromLocationName(location, 0);
    } catch (IOException e) {
        e.printStackTrace();
    }

However, here, if there was an exception, addressList is still null, therefore your error.

    Address address = addressList.get(0);

Without seeing the actual exception, hard to give a complete answer, but the quick solution to prevent the NullPointer is to move that last line into the try (and optionally check that !addressList.isEmpty() before attempting to access its data).

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245