title: Google Analytics Android error java.lang.NullPointerException
body: <p>I'm trying to work on Google Analytics for Androi. I get the message, and I'm following the <a href="https://developers.google.com/analytics/devguides/collection/android/v2/advanced" rel="nofollow">advanced configuration of Android Analytics in Google Android SDK</a>.</p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity 
    ComponentInfo{com.example.secondapp/com.example.secondapp.MainActivity}:
    java.lang.NullPointerException
  </code></pre>
  
  <p>And I get this error: </p>
  
  <pre><code>01-02 15:39:38.410: D/AndroidRuntime(1338): Shutting down VM
  01-02 15:39:38.410: W/dalvikvm(1338): threadid=1: thread exiting with uncaught exception (group=0xb1ab7ba8)
  01-02 15:39:38.420: E/AndroidRuntime(1338): FATAL EXCEPTION: main
  01-02 15:39:38.420: E/AndroidRuntime(1338): Process: com.example.secondapp, PID: 1338
  01-02 15:39:38.420: E/AndroidRuntime(1338): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.secondapp/com.example.secondapp.MainActivity}: java.lang.NullPointerException
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at android.os.Handler.dispatchMessage(Handler.java:102)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at android.os.Looper.loop(Looper.java:136)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at android.app.ActivityThread.main(ActivityThread.java:5017)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at java.lang.reflect.Method.invoke(Method.java:515)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at dalvik.system.NativeStart.main(Native Method)
  01-02 15:39:38.420: E/AndroidRuntime(1338): Caused by: java.lang.NullPointerException
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at com.example.secondapp.MainActivity.onCreate(MainActivity.java:24)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at android.app.Activity.performCreate(Activity.java:5231)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
  01-02 15:39:38.420: E/AndroidRuntime(1338):     ... 11 more
  </code></pre>
  
  <p>Here is my code for <code>SecondAppApplication</code>, which has similarities to the android docs:</p>
  
  <pre><code>public class SecondAppApplication extends Application {
  
      private static GoogleAnalytics mGa;
      private static Tracker mTracker;
  
      private static final String GA_PROPERTY_ID = "UA-XXXXXXXX-1";
      private static final int GA_DISPATCH_PERIOD = 30;
      private static final boolean GA_IS_DRY_RUN = false;
      private static final LogLevel GA_LOG_VERBOSITY = LogLevel.INFO;
      private static final String TRACKING_PREF_KEY = "trackingPreferences";
  
      @SuppressWarnings("deprecation")
      private void initializeGa() {
          mGa = GoogleAnalytics.getInstance(this);
          mTracker = mGa.getTracker(GA_PROPERTY_ID);
  
          GAServiceManager.getInstance().setLocalDispatchPeriod(GA_DISPATCH_PERIOD);
  
          mGa.setDryRun(GA_IS_DRY_RUN);
  
          mGa.getLogger().setLogLevel(GA_LOG_VERBOSITY);
  
          SharedPreferences userPrefs = PreferenceManager.getDefaultSharedPreferences(this);
          userPrefs.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
              @Override
              public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                  if (key.equals(TRACKING_PREF_KEY)) {
                      GoogleAnalytics.getInstance(getApplicationContext()).setAppOptOut(sharedPreferences.getBoolean(key, false));
                  }
              }
          });
  
      }
  
      @Override
      public void onCreate() {
          super.onCreate();
          initializeGa();
      }
  
      public static Tracker getGaTracker() {
          return mTracker;
      }
  
      public static GoogleAnalytics getGaInstance() {
          return mGa;
      }
  
  }
  </code></pre>
  
  <p>Here is my <code>MainActivity</code> class:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
      private static final String SCREEN_LABEL = "Main Screen";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          SecondAppApplication.getGaTracker().set(Fields.SCREEN_NAME,SCREEN_LABEL);
          setContentView(R.layout.main);
      }
  
      @Override
      public void onStart() {
          super.onStart();
          SecondAppApplication.getGaTracker().send(MapBuilder.createAppView().build());
      }
  
      @Override
      public void onStop() {
          super.onStop();
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.main, menu);
          return super.onCreateOptionsMenu(menu);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch(item.getItemId()) {
              case R.id.action_search:
                  openSearch();
                  return true;
              case R.id.action_settings:
                  openSettings();
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
      }
  
      public void openSearch() {
  
      }
  
      public void openSettings() {
  
      }
  
  
      public void sendMessage(View view) {
          Intent intent = new Intent(this,DisplayMessageActivity.class);
          EditText editText = (EditText) findViewById(R.id.edit_message);
          String message = editText.getText().toString();
          intent.putExtra(EXTRA_MESSAGE, message);
          startActivity(intent);
      }
  
  }
  </code></pre>
  
  <p>Why does <code>java.lang.NullPointerException</code> exists, and/or how would I solve this? </p>
  
  <p>Do I need to declare <code>initializeGa()</code> again inside <code>MainActivity</code> or <code>AndroidManifest.xml</code>?</p>