My last question for the near future .. here is the code for ScreenOne

package com.example.app; import android.app.Activity; import android.location.Location; import android.os.Bundle; import android.view.View; import android.widget.TextView; import java.util.Arrays; public class ScreenOne extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.screen_one); } GeoPoint currentLocation = new GeoPoint(formatLocation(null), formalLoc(null), "hhh"); private double formalLoc(Location location) { return location.getLongitude(); } private double formatLocation(Location location) { return location.getLatitude(); } GeoPoint locations[] = new GeoPoint[]{ new GeoPoint(55.11, 37.11, "N1"), new GeoPoint(55.22, 37.22, "N2"), new GeoPoint(55.33, 37.33, "N3"), }; GeoPoint nearest = GeoPoint.getNearestLocation(currentLocation, Arrays.asList(locations)); public void onClick(View view) { TextView helloTextView = (TextView) findViewById(R.id.station_name); helloTextView.setText(nearest.getName()); } } 

The application crashes when it starts. (Closes abnormally). what is the problem? GeoPoint, maybe with it? ..

 package com.example.app; import android.location.Location; import java.util.Collection; public class GeoPoint { public final double lat; public final double lon; public String name; public GeoPoint(double lat, double lon, String name) { this.lat = lat; this.lon = lon; this.name = name; } public String getName(){ return name; } public GeoPoint(Location location) { this.lat = location.getLatitude(); this.lon = location.getLongitude(); } public static GeoPoint getNearestLocation(GeoPoint current, Collection<GeoPoint> locations) { GeoPoint res = null; float lastDisance = Float.MAX_VALUE; float locDistance[] = new float[1]; for (GeoPoint loc: locations) { Location.distanceBetween(current.lat, current.lon, loc.lat, loc.lon, locDistance); if (res == null || locDistance[0] < lastDisance) { res = loc; lastDisance = locDistance[0]; } } return res; } } 

Closed due to the fact that it is off-topic by the participants Athari , VenZell , alexis031182 , fori1ton , Pavel Mayorov 18 May '15 at 8:18 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "Questions asking for help with debugging (" why does this code not work? ") Should include the desired behavior , a specific problem or error, and the minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, complete, repeatable example . " - Athari, VenZell, alexis031182, fori1ton
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • what does the log write? . - arg
  • one
    Did you look at stacceris? There is a line in which the error, and the type of error. - Vladyslav Matviienko
  • already figured out .. only now is another question. Now we will set a new one .. - DenShDen

1 answer 1

Obviously, because of this static initialization:

 GeoPoint currentLocation = new GeoPoint(formatLocation(null), formalLoc(null), "hhh"); 

Here you use the formatLocation and formatLoc methods to which you pass null , but you don’t check in the method itself whether a parameter of the type Location is null'ом .

Hence the error - NullPointerException .

And, indeed, look in stacktrace - you can correct these ridiculous mistakes yourself.

  • so, now it will be .. - DenShDen