1
votes

I'm trying to implement GA Campaign Measurement following by this link here https://developers.google.com/analytics/devguides/collection/android/v2/campaigns#campaign-params

So, I've completed everything, but it gives NullpointerException in my code.

// Get the intent that started this Activity.
        Intent installIntent = this.getIntent();
        Uri Installuri = installIntent.getData();

        EasyTracker.getInstance().setContext(this);
        if (installIntent != null) {
            EasyTracker.getTracker().setCampaign(Installuri.getPath()); //line 82
          }

That is the code, which should do the General Tracking. Nothing special. However on line 82, which is mentioned above it throws NullPointerException. Here is the logcat:

06-23 19:54:54.864: E/AndroidRuntime(13773):    at android.os.Looper.loop(Looper.java:137)
06-23 19:54:54.864: E/AndroidRuntime(13773):    at android.app.ActivityThread.main(ActivityThread.java:4441)
06-23 19:54:54.864: E/AndroidRuntime(13773):    at java.lang.reflect.Method.invokeNative(Native Method)
06-23 19:54:54.864: E/AndroidRuntime(13773):    at java.lang.reflect.Method.invoke(Method.java:511)
06-23 19:54:54.864: E/AndroidRuntime(13773):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-23 19:54:54.864: E/AndroidRuntime(13773):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-23 19:54:54.864: E/AndroidRuntime(13773):    at dalvik.system.NativeStart.main(Native Method)
06-23 19:54:54.864: E/AndroidRuntime(13773): Caused by: java.lang.NullPointerException
06-23 19:54:54.864: E/AndroidRuntime(13773):    at uk.co.futurelite.elite.drum.machine.Main.onCreate(Main.java:82)
06-23 19:54:54.864: E/AndroidRuntime(13773):    at android.app.Activity.performCreate(Activity.java:4465)
06-23 19:54:54.864: E/AndroidRuntime(13773):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-23 19:54:54.864: E/AndroidRuntime(13773):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
06-23 19:54:54.864: E/AndroidRuntime(13773):    ... 11 more

So, I check it with IF statement if it is not null like:

if (installIntent != null) {

so, what happens that it throws NullPointerException? when it passes the IF statement.

PS. I did finish it until the end and though probably I've to pass something to the broadcastReceiver, I can see in logcat that it receives the campaign successfully as follows:

06-23 19:54:56.544: W/GAV2(13773): Thread[GAThread,5,main]: Service unavailable (code=1), will retry.
06-23 19:54:56.824: I/GAV2(13773): Thread[GAThread,5,main]: Campaign found: utm_source%3Dxdebugx%26utm_medium%3Dbutton

But the app itself crashes and doesnt start.

1
Can you tell us what is null please ? Is it EasyTracker.getTracker() ? - Stephane Mathis
@StephaneMathis, according to logcat the line 82 throws NullPointer. and it is EasyTracker.getTracker().setCampaign(Installuri.getPath()); this line - Daler
Yeah, but what in this line ? Because getTracker() can return null, then setCampain on a null objet give you also a NullPointerException. - Stephane Mathis
@StephaneMathis, that is the simple copy-paste from Google Docs above, I call that getTracker first time in the app. I don't have any other getTracker in the app - Daler

1 Answers

1
votes

You need to check that the URI is not null, before you try to call .getPath(). In the documentation you mentioned, it is written like this:

if (intent.getData() != null) {
  EasyTracker.getTracker().setCampaign(uri.getPath());
}

Using the style in your code snippet, it should look like this:

// Get the intent that started this Activity.
Intent installIntent = this.getIntent();
Uri Installuri = installIntent.getData();

EasyTracker.getInstance().setContext(this);
if (Installuri != null) {
    EasyTracker.getTracker().setCampaign(Installuri.getPath()); //line 82
}