title: How to Run Static Inner Class in Android App
body: <p>I'm developing an Android app. I've looked online, and tried a few things, but haven't gotten anything to work. I'm trying to run a static inner class in my Android app. It sorts an ArrayList for me. I tried the following:</p>
  
  <pre><code>GenericCachedSorter sort = new GenericCachedSorter();
  sort.main(Longitude1);
  sort.sort(distancetos, null);
  </code></pre>
  
  <p>and </p>
  
  <pre><code>new GenericCachedSorter();
  GenericCachedSorter.main(Longitude1);
  </code></pre>
  
  <p>But both gave NPE on those lines of code. I'm not sure why this isn't working. My code is below:</p>
  
  <p><strong>NearestStations.java</strong></p>
  
  <pre><code>public class Neareststations extends Activity implements LocationListener {
  LocationManager mLocationManager;
  
  double longitude;
  double latitude;
  
  String distancea;
  String distance1;
  static Map&lt;String, String&gt; myMap1 = new HashMap&lt;String, String&gt;();
  
  static ArrayList&lt;Double&gt; distancetos = new ArrayList&lt;Double&gt;();
  
  @SuppressLint("NewApi")
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.neareststations);
      // Show the Up button in the action bar.
      getActionBar().setDisplayHomeAsUpEnabled(true);
  
      List&lt;String&gt; Lat1 = Arrays.asList(getResources().getStringArray(R.array.Latitude));
      Log.i("Lat1", String.valueOf(Lat1.get(0)));
  
      String[] Stations = getResources().getStringArray(R.array.Stations);
      String[] Longitude = getResources().getStringArray(R.array.Longitude);
  
      for (int h = 0; h &lt;144; h++) {
          myMap1.put(Stations[h], Longitude[h]);
  
      }
  
  
  
          mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  
          Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
          longitude = location.getLongitude();
          latitude = location.getLatitude();
          if(location != null &amp;&amp; location.getTime() &gt; Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) {
              {
  
          }
          }
          else {
              mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 50, this);
              Log.i("MYTAG", String.valueOf(location));
          }
  
          longitude = location.getLongitude();
          latitude = location.getLatitude();
          if (location != null) {
  
              longitude = location.getLongitude();
              latitude = location.getLatitude();
              Log.i("MYTAG", String.valueOf(longitude));
              Log.i("MYTAG", String.valueOf(latitude));
  
  
              List&lt;String&gt; Long = Arrays.asList(getResources().getStringArray(R.array.Longitude));
              List&lt;String&gt; Lat = Arrays.asList(getResources().getStringArray(R.array.Latitude));
  
              Log.i("Long1", String.valueOf(Long.get(0)));
  
              Iterator&lt;String&gt; iterator = Lat.iterator();
              Iterator&lt;String&gt; iterator1 = Long.iterator();
  
              ArrayList&lt;Double&gt; longitudeArray = new ArrayList&lt;Double&gt;();
  
              while(iterator.hasNext()){
              for (int i=0; i&lt;144;i++){
  
              double distance = 0;  
  
              double lat_end = 0;
              double lon_end = 0;
  
  
              try {
                  lat_end = Double.parseDouble(iterator.next());
                  lon_end = Double.parseDouble(iterator1.next());
                  longitudeArray.add((lon_end));
  
              } catch (NumberFormatException e) {
                  Log.v("Main", "Convert to Double Failed : ");
              }
  
              Location locationA = new Location("point A");  
              locationA.setLatitude(latitude);  
              locationA.setLongitude(longitude);  
  
              Location locationB = new Location("point B");  
              locationB.setLatitude(lat_end);  
              locationB.setLongitude(lon_end);  
  
              distance = locationA.distanceTo(locationB) * 0.000621371192237334;
  
              String dista = Double.toString(distance);
  
  
              distancetos.add(Double.parseDouble(dista));
              }
              }
  
  
                  Collections.sort(distancetos);
  
                  distancea = distancetos.get(0).toString();
                  distance1 = distancetos.get(1).toString();
  
                  String Longa = longitudeArray.get(0).toString();
                  String Long1 = longitudeArray.get(1).toString();                
  
              String[] Stations1 = getResources().getStringArray(R.array.Stations);
              String[] Longitude1 = getResources().getStringArray(R.array.Longitude);
              String[] Latitude = getResources().getStringArray(R.array.Latitude);
  
  
  
              Map&lt;String, String&gt; myMap = new HashMap&lt;String, String&gt;();{
              for (int i = 0; i &lt;144; i++) {
                  myMap.put(Latitude[i], Stations1[i]);
              }
              }
  
              Map&lt;String, String&gt; myMap1 = new HashMap&lt;String, String&gt;();{
              for (int h = 0; h &lt;144; h++) {
                  myMap1.put(Longitude1[h], Stations1[h]);
  
              }
              }
  
              String value = myMap1.get(Longa);
      }
      }
  
      public void onLocationChanged(Location location) {
      }
  
  
      public void onProviderDisabled(String arg0) {}
      public void onProviderEnabled(String arg0) {}
      public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
  
  public static class GenericCachedSorter {
  public static void main(String[] args) {
          sort(distancetos, new ToComparable&lt;Double, Double&gt;() {
              @Override
              public Double toComparable(Double distance) {
                  // return the longitude associated with this distance
                  return (Double.parseDouble(myMap1.get(distance)));
              }
          });
  
          for (Double distance : distancetos)
              System.out.println(distancetos);
      }
  
    public interface ToComparable&lt;T, C extends Comparable&lt;? super C&gt;&gt; {
      C toComparable(T t);
    }
  
      public static &lt;T, C extends Comparable&lt;? super C&gt;&gt; void sort(List&lt;T&gt; list, ToComparable&lt;T, C&gt; function) {
         class Pair implements Comparable&lt;Pair&gt; {
            final T original;
            final C comparable;
  
            Pair(T original, C comparable) {
               this.original = original;
               this.comparable = comparable;
            }
  
            @Override
            public int compareTo(Pair other) {
                  return
                    comparable == null &amp;&amp; other.comparable == null ? 0 :
                    comparable == null ? -1 :
                    other.comparable == null ? 1 :
                    comparable.compareTo(other.comparable);
            }
         }
  
         List&lt;Pair&gt; pairs = new ArrayList&lt;Pair&gt;(list.size());
         for (T original : list)
            pairs.add(new Pair(original, function.toComparable(original)));
  
         Collections.sort(pairs);
  
         ListIterator&lt;T&gt; iter = list.listIterator();
         for (Pair pair : pairs) {
            iter.next();
            iter.set(pair.original);
         }
      }
  }
  }
  </code></pre>
  
  <p>Thank you for your help.</p>