title: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
body: <p>I am trying to envelop my data(Longitude, Latititude, route,timeStamp) in JSON format with the help of GSON library to be sent  to the server after 60 seconds.  At the moment I am getting the following error <code>java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference</code>. I am getting these variables(pLong,pLat, route, formatted) displayed in the xml file.</p>
  
  <p>I appreciate any help.</p>
  
  <p>Error:</p>
  
  <pre><code>04-21 23:15:02.893: E/AndroidRuntime(685): FATAL EXCEPTION: main
  04-21 23:15:02.893: E/AndroidRuntime(685): Process: com.bustracker, PID: 685
  04-21 23:15:02.893: E/AndroidRuntime(685): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x4000010 } in com.bustracker.MainActivity$BroadcastReceiverListener@2d9405c7
  04-21 23:15:02.893: E/AndroidRuntime(685):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:933)
  04-21 23:15:02.893: E/AndroidRuntime(685):  at android.os.Handler.handleCallback(Handler.java:739)
  04-21 23:15:02.893: E/AndroidRuntime(685):  at android.os.Handler.dispatchMessage(Handler.java:95)
  04-21 23:15:02.893: E/AndroidRuntime(685):  at android.os.Looper.loop(Looper.java:145)
  04-21 23:15:02.893: E/AndroidRuntime(685):  at android.app.ActivityThread.main(ActivityThread.java:5944)
  04-21 23:15:02.893: E/AndroidRuntime(685):  at java.lang.reflect.Method.invoke(Native Method)
  04-21 23:15:02.893: E/AndroidRuntime(685):  at java.lang.reflect.Method.invoke(Method.java:372)
  04-21 23:15:02.893: E/AndroidRuntime(685):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
  04-21 23:15:02.893: E/AndroidRuntime(685):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
  04-21 23:15:02.893: E/AndroidRuntime(685): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
  04-21 23:15:02.893: E/AndroidRuntime(685):  at com.bustracker.MainActivity$BroadcastReceiverListener.parse_SSID(MainActivity.java:218)
  04-21 23:15:02.893: E/AndroidRuntime(685):  at com.bustracker.MainActivity$BroadcastReceiverListener.onReceive(MainActivity.java:172)
  04-21 23:15:02.893: E/AndroidRuntime(685):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:923)
  04-21 23:15:02.893: E/AndroidRuntime(685):  ... 8 more
  </code></pre>
  
  <p>Data class:</p>
  
  <pre><code>public class Data {
      double longitude;
      double latitude;
      //int speed;
      String time;
      int route;
  
      public Data(double longitude, double latitude, String time,
              int route) {
          super();
          this.longitude = longitude;
          latitude = latitude;
          //this.speed = speed;
          this.time = time;
          this.route = route;
      }
  
      public double getLongitude() {
          return longitude;
      }
  
      public void setLongitude(double longitude) {
          this.longitude = longitude;
      }
  
      public double getLatitude() {
          return latitude;
      }
  
      public void setLatitude(double latitude) {
          latitude = latitude;
      }
  
  //  public int getSpeed() {
  //      return speed;
  //  }
  //
  //  public void setSpeed(int speed) {
  //      this.speed = speed;
  //  }
  
      public String getTime() {
          return time;
      }
  
      public void setTime(String time) {
          this.time = time;
      }
  
      public int getRoute() {
          return route;
      }
  
      public void setRoute(int route) {
          this.route = route;
      }
  
  }
  </code></pre>
  
  <p>DataSerializer class:</p>
  
  <pre><code>import java.lang.reflect.Type;
  
  import com.google.gson.JsonElement;
  import com.google.gson.JsonObject;
  import com.google.gson.JsonPrimitive;
  import com.google.gson.JsonSerializationContext;
  import com.google.gson.JsonSerializer;
  
  public class DataSerializer implements JsonSerializer&lt;Data&gt; {
  
      @Override
      public JsonElement serialize(Data data, Type arg1,
              JsonSerializationContext arg2) {
           JsonObject result = new JsonObject();
           result.add("longitude", new JsonPrimitive(data.getLongitude()));
           result.add("latitude", new JsonPrimitive(data.getLatitude()));
          // result.add("speed", new JsonPrimitive(data.getSpeed()));
           result.add("formatted", new JsonPrimitive(data.getTime()));
           result.add("route", new JsonPrimitive(data.getRoute()));
          return result;
      }
  
  }
  </code></pre>
  
  <p>This part is in the MainActivity:</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
      double pLong;
      double pLat;
      String formatted;
      int route_number;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Data d = new Data(pLong, pLat, formatted,route_number);
          Gson gson = new GsonBuilder().registerTypeAdapter(Data.class, new DataSerializer()).create();
          System.out.println(gson.toJson(d));
    }
  }
  </code></pre>