0

I'm hoping someone could lead me in the right direction. Today i noticed that my application stoped working when trying to use google maps. My application has worked flawless. I have not updated anything in the code. It simply just stoped working.

here is the debug:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
                                                                   at se.xflash.tidsboken.CreateNewJob.onMapReady(CreateNewJob.java:725)
                                                                   at com.google.android.gms.maps.MapFragment$zza$1.zza(Unknown Source)
                                                                   at com.google.android.gms.maps.internal.zzl$zza.onTransact(Unknown Source)
                                                                   at android.os.Binder.transact(Binder.java:387)
                                                                   at com.google.android.gms.maps.internal.v$a$a.a(:com.google.android.gms.alldynamite:82)
                                                                   at maps.ei.bu$6.run(Unknown Source)
                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:224)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5514)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I dont understand why i all of the sudden get a NullPointerException, it dosnt make any sence!

here is a snippet of the code where it complains.

public void onMapReady(GoogleMap map) {


    DatabaseHandlerSettings db = new DatabaseHandlerSettings(getActivity());
    PersonInfoDatabase info = db.getContact(1);
    String adressInfo = info.getAdress();
    String ortInfo = info.getOrt();


    String adress = (adressInfo + ", " + ortInfo);

    Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
    List<Address> addresses = null;

    try {
        addresses = geocoder.getFromLocationName(adress, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Address address = addresses.get(0);
    double longitudeStart = address.getLongitude();
    double latitudeStart = address.getLatitude();

    LatLng location = new LatLng(latitudeStart, longitudeStart);
    map.setMyLocationEnabled(true);
    map.getUiSettings().setZoomControlsEnabled(true);


   map.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 12));

I mean, how can my applications stop working out of the blue? its on the market. I understand if you don't understand what i mean.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kazu
  • 67
  • 2
  • 10
  • 1
    you should log and analyze the exception and not just `try catch e.printStackTrace();` Often it does not make any sense to go on, when an exception occurs... so I assume the `NullpointerException` is a subsequent error. – Meiko Rachimow Apr 03 '16 at 19:03
  • 1
    The issue has nothing to do with the implementation of `GoogleMap`. As per your logs, the app is crashing when it is trying to execute the method `get(int)`. The code you have uploaded does not have that part. So may be you can add the correct part of the code in your question. – Saumik Bhattacharya Apr 03 '16 at 19:06
  • @SaumikBhattacharya The code in the question actually does contain the offending line, it's this one: `Address address = addresses.get(0);` – Daniel Nugent Apr 03 '16 at 19:07
  • @DanielNugent , yes true! – Saumik Bhattacharya Apr 03 '16 at 19:09
  • Im guessing it has something to do with the database? but what i dont understand is how can my app been working for several months, and today just not working? – Kazu Apr 03 '16 at 19:11
  • @user1512762 This call that you're making requires an internet connection so that it can reach the webservice. If a device has no internet connection while using your app, this will happen. You can test this by running your app on a device that is in airplane mode, you should be able to reproduce the issue like that. Just do a null check on `addresses`, and if it's null show an error status (or whatever you want to happen in the error case) – Daniel Nugent Apr 03 '16 at 19:15
  • Im testing on my phone. It has internet access. So perhaps the problem is withing the new android runtime permissions? Im guessing thats the problem. However, i managed to make the app show the "world" on the devices, so it has internet permissions. The app dosnt realy use the phones location becouse the coordinates are hardcoded into the databases. – Kazu Apr 03 '16 at 19:20

1 Answers1

1

Following your code:

List<Address> addresses = null;

try {
    addresses = geocoder.getFromLocationName(adress, 1);
} catch (IOException e) {
    e.printStackTrace();
}
Address address = addresses.get(0);

In case there is IOException - addresses list will still be null. So calling addresses.get(0) will cause NullPointerException.

localhost
  • 5,568
  • 1
  • 33
  • 53
  • Thank you. This morning the application was working again without me even changing the code. So im guessing the problem was somewhere else, however, i will look into this NullPointer for when the next time this hapens. thanks – Kazu Apr 04 '16 at 04:34