Null point exception when accessing some methods TomTomMap?

Hi all,

I was trying to create a map, add a pin/ballon of the given location and center the location in the map.
However when I use centeron (I know it is depreciated now) it gives an error null point exception. It is same as when I tried to access other TomTomMap method.

Without accessing the method the fragment shows the map without any problem.

I thought that perhaps is my code problem (as I am a beginner). However when I tried the Search Along the route tutorial as example it also give me similar issue

Attempt to invoke interface method ‘com.tomtom.online.sdk.map.MarkerSettings com.tomtom.online.sdk.map.TomtomMap.getMarkerSettings()’ on a null object reference

Is that something I missed ? or anything I can do to debug?

Thanks.

Hi.

I’ve just checked Search Along the Route tutorial and it works fine, so we will need more details from you to resolve your issue.
Please double check your implementation of the
public void onMapReady(@NonNull final TomtomMap tomtomMap) so that you will be sure that you are not using a TomTom map object before it was properly initialized.

Regards,
Mateusz

2 Likes

Hi Mateusz,

Thanks for your help and yes onMapReady was missing final there.

Moreover, just wonder since I declared TomtomMap in the class, I assume it can be access by all methods within the class? Because I have some customized methods or try to call say tomtomMap.getUiSettings().setMapTilesType(MapTilesType.VECTOR); in other methods, it still causing null point exception sometimes.

I also noticed that after onMapReady the class “global” tomtomMap become null again.

Hi.

It’s hard to guess what can be wrong with your code.
Please share the code for your “customized methods” (or for the whole activity class) so that we’ll be able to help you better.

Regards,
Mateusz

Hi Mateusz,

Really appreciate your help and patient.

After further try this and that, the reason why I get null point exception is other code/methods want to use tomtomMap before OnMapReady finished its tasks.

Hopefully is my last question regarding this issue :joy: :joy: : Are there way onMapReady can tell the class the map is ready and continue the rest of the task?

Thanks again.

Regards,

DS.

Hi.

Once your onMapReady method is called by the SDK, your class instantly knows, that the map is ready.
The golden rule is: You shall not use the map, up until the onMapReady has been called. :wink:
In short, it means that you should remove code from methods that are called before, like for example, constructors, onCreate, onStart, etc.

Take a look at our Search along the route or Time to leave tutorials and study the code. It should become clear.

Regards,
Mateusz

1 Like