Search code examples
javaandroidnullpointerexceptionsdknavigation

Google Cloud Nav SDK error on Android intagration NullPointerException: Attempt to invoke virtual method on a null object reference


I am trying to integrate Google Cloud Navigation SDK for Android. Followed all steps in the documentation - https://developers.google.com/maps/documentation/navigation/android-sdk/billing. When I call NavigationTransactionRecorder.pickup() method I get the NullPointerException although none of two parameters (waypoint and transaction ID) are null.

NavigationTransactionRecorder transactionRecorder = NavigationApi.getTransactionRecorder(getApplication());

        pickUpButton.setOnClickListener(new View.OnClickListener() {

                                            @Override
                                            public void onClick(View v) {

                                               

        
                                                Waypoint waypoint = Waypoint.builder().setLatLng(51.434110, -0.139030)
                                                        .setPreferSameSideOfRoad(true)
                                                        .setTitle("pickup waypoint")
                                                        .build();
                                    
                                                
                                                try {
                                                    transactionRecorder.pickup(

                                                            waypoint,
                                                            Arrays.asList(NavigationTransactionRecorder.generateTransactionId()));

                                                } catch (
                                                        NavigationTransactionRecorder.TransactionException e) {
                                                    throw new RuntimeException(e);
                                                }

                                            }
                                        }
        );

This is the error message below:


 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.libraries.navigation.NavigationTransactionRecorder.pickup(com.google.android.libraries.navigation.Waypoint, java.util.List)' on a null object reference
                                                                                                        at com.example.myapplication.MainActivity$1.onClick(MainActivity.java:56)
                                                                                                        at android.view.View.performClick(View.java:7506)
                                                                                                        at android.view.View.performClickInternal(View.java:7483)
                                                                                                        at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
                                                                                                        at android.view.View$PerformClick.run(View.java:29334)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                        at android.os.Looper.loop(Looper.java:288)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7872)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

I checked both parameters to be null and they are not null. I keep getting this error


Solution

  • According to this,

    This class should be used if and only if your company is billed by Google on a per-transaction basis.

    A Navigator must be successfully obtained before calling this method, otherwise it will return null.

    If your project configuration prevents calling this method, it will return null.

    It seems like one of these reasons could be the culprit.