0

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:

GenericCachedSorter sort = new GenericCachedSorter();
sort.main(Longitude1);
sort.sort(distancetos, null);

and

new GenericCachedSorter();
GenericCachedSorter.main(Longitude1);

But both gave NPE on those lines of code. I'm not sure why this isn't working. My code is below:

NearestStations.java

public class Neareststations extends Activity implements LocationListener {
LocationManager mLocationManager;

double longitude;
double latitude;

String distancea;
String distance1;
static Map<String, String> myMap1 = new HashMap<String, String>();

static ArrayList<Double> distancetos = new ArrayList<Double>();

@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<String> 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 <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 && location.getTime() > 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<String> Long = Arrays.asList(getResources().getStringArray(R.array.Longitude));
            List<String> Lat = Arrays.asList(getResources().getStringArray(R.array.Latitude));

            Log.i("Long1", String.valueOf(Long.get(0)));

            Iterator<String> iterator = Lat.iterator();
            Iterator<String> iterator1 = Long.iterator();

            ArrayList<Double> longitudeArray = new ArrayList<Double>();

            while(iterator.hasNext()){
            for (int i=0; i<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<String, String> myMap = new HashMap<String, String>();{
            for (int i = 0; i <144; i++) {
                myMap.put(Latitude[i], Stations1[i]);
            }
            }

            Map<String, String> myMap1 = new HashMap<String, String>();{
            for (int h = 0; h <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<Double, Double>() {
            @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<T, C extends Comparable<? super C>> {
    C toComparable(T t);
  }

    public static <T, C extends Comparable<? super C>> void sort(List<T> list, ToComparable<T, C> function) {
       class Pair implements Comparable<Pair> {
          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 && other.comparable == null ? 0 :
                  comparable == null ? -1 :
                  other.comparable == null ? 1 :
                  comparable.compareTo(other.comparable);
          }
       }

       List<Pair> pairs = new ArrayList<Pair>(list.size());
       for (T original : list)
          pairs.add(new Pair(original, function.toComparable(original)));

       Collections.sort(pairs);

       ListIterator<T> iter = list.listIterator();
       for (Pair pair : pairs) {
          iter.next();
          iter.set(pair.original);
       }
    }
}
}

Thank you for your help.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
hichris123
  • 10,145
  • 15
  • 56
  • 70

2 Answers2

1

public static void main(String[] args) is not available in Android. Remove it and call it in another simple method.

    public void SortingHere() {
    sort(distancetos, new ToComparable<Double, Double>() {
        @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);
}
VenomVendor
  • 15,064
  • 13
  • 65
  • 96
  • I fixed it with your solution. I'm using `GenericCachedSorter sort = new GenericCachedSorter(); sort.SortingHere();` to start it, but I'm now getting an exception on this line `pairs.add(new Pair(original, function.toComparable(original)));` – hichris123 Oct 31 '13 at 22:50
0

VenomVendor has most of it right.

You're only allowed to have one public class per java file. This is a design decision that was made in Java. And in your case, the main public class and the main entry point into this file is already Neareststations.

Community
  • 1
  • 1
Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49
  • So, will it give me an exception? – hichris123 Oct 29 '13 at 00:12
  • @hichris123 A bit of mis reading to call static method you have call ClassName.MethodName(arguments); in your case `GenericCachedSorter.main(Longitude1)` you need not call `new GenericCachedSorter();` – VenomVendor Oct 29 '13 at 00:23
  • Okay, I'm using `GenericCachedSorter sort = new GenericCachedSorter(); sort.SortingHere();` but I'm now getting an exception on this line `pairs.add(new Pair(original, function.toComparable(original)));` – hichris123 Oct 29 '13 at 00:28