title: Google Map returning nullpointerexception Google Maps Android V2
body: <p>I have a <code>google map</code> in a <code>fragment</code> in my application. Funny thing is that the map loads, but the logcat displays a <code>NullPointerException</code>, and I only noticed it when tried to update the camera on the map so that it could automatically zoom in on the <code>marker</code>. Only then the map will crash. Otherwise, the map loads fine in the <code>fragment</code> but with no <code>marker</code> (even though I am adding one using my current <code>gps location</code>). </p>
  
  <p>EDITED*</p>
  
  <hr>
  
  <p>I understand that its crashing because the <code>googleMap</code> is null, but I don't understand why it's returning null. The map renders and well I've done this before and it usually crashes at that point. Doesn't render. </p>
  
  <hr>
  
  <p>Here is the logcat error</p>
  
  <pre><code>05-08 01:30:33.143  30130-30130/com.example.javed_000.famjam E/mapApp﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
  05-08 01:30:33.152  30130-30130/com.example.javed_000.famjam E/MapFragment﹕ lat = 10.7270913 and Long = -61.5544451
  05-08 01:30:33.155  30130-30130/com.example.javed_000.famjam D/AndroidRuntime﹕ Shutting down VM
  05-08 01:30:33.156  30130-30130/com.example.javed_000.famjam E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: com.example.javed_000.famjam, PID: 30130
  java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.animateCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
          at com.example.javed_000.famjam.MapFragment.getLocation(MapFragment.java:60)
          at com.example.javed_000.famjam.MapFragment.onCreateView(MapFragment.java:33)
          at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
          at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
          at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
          at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
          at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
          at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
          at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555)
          at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
          at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
          at com.example.javed_000.famjam.SlidingTabLayout$TabClickListener.onClick(SlidingTabLayout.java:283)
          at android.view.View.performClick(View.java:4780)
          at android.view.View$PerformClick.run(View.java:19866)
          at android.os.Handler.handleCallback(Handler.java:739)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5254)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  </code></pre>
  
  <p>This is my manifest file   </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.javed_000.famjam" &gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
      &lt;uses-permission android:name="android.permission.USE_CREDENTIALS" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
      &lt;uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
  
          &lt;meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /&gt;
  
          &lt;meta-data
              android:name="com.google.android.maps.v2.API_KEY"
              android:value="AIzaSyAhgnZ7ESL-EIRS4Da_hGw_7u9IQI3SZE0" /&gt;
  
          &lt;activity
              android:name=".SignInActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>This is my dependencies in the gradle file </p>
  
  <pre><code>dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.android.support:appcompat-v7:22.0.0'
      compile 'com.google.android.gms:play-services:7.0.0'
      compile 'com.google.android.gms:play-services-maps:7.0.0'
  }
  </code></pre>
  
  <p>This is my layout file. </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;fragment
          android:id="@+id/mapView"
          android:name="com.google.android.gms.maps.SupportMapFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Here is my code snippet from the Map Fragment </p>
  
  <pre><code>private void createMapView(){
      try {
          if(googleMap == null){
              SupportMapFragment smf = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapView);
              googleMap = smf.getMap();
  
              if(googleMap == null) {
                  Toast.makeText(getActivity().getBaseContext(),
                          "Error creating map", Toast.LENGTH_SHORT).show();
              }
          }
      } catch (NullPointerException exception){
          Log.e("mapApp", exception.toString());
      }
  }
  
  private void getLocation(){
      gps = new GPSTracker(this.getActivity());
      if(gps.CanGetLocation()){
          latitude = gps.getLatitude();
          longitude = gps.getLongitude();
          Log.e("MapFragment","lat = " + latitude + " and Long = " + longitude);
          addMarker(latitude,longitude);
          googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude), 12));
      } else {
          gps.showSettingsAlert();
      }
  }
  
  private void addMarker(double lat, double lng){
      if(googleMap != null){
          googleMap.addMarker(new MarkerOptions()
                          .position(new LatLng(lat, lng))
                          .title("Marker")
                          .draggable(true)
          );
      }
  }
  </code></pre>