Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1016331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:32:02+00:00 2026-05-16T10:32:02+00:00

I’ve got the following MapOverlay . The following line is throwing a NullPointer but

  • 0

I’ve got the following MapOverlay. The following line is throwing a NullPointer but I don’t understand why :S mapView.getProjection().toPixels(p, screenPts); could anyone help me out?

    public class Mapview extends MapActivity {
GeoPoint p;
MapView mapView; 
MapController mc;

@Override
protected boolean isRouteDisplayed() {
 return false;
}
public int icount = 0;
MapController mapController;
MyPositionOverlay positionOverlay;
private LocationData tweets;
public int start = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);



MapView myMapView = (MapView)findViewById(R.id.mapView);

myMapView.setSatellite(true);
myMapView.setStreetView(true);
myMapView.displayZoomControls(false);
mapController = myMapView.getController();

mapController.setZoom(17);
myMapView.setBuiltInZoomControls(true);

//Adding points here


//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = myMapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);        

myMapView.invalidate();

// Add the MyPositionOverlay
positionOverlay = new MyPositionOverlay();
List<Overlay> overlays = myMapView.getOverlays();
overlays.add(positionOverlay);


LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
String provider = locationManager.getBestProvider(criteria, true);


locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);

}


private final LocationListener locationListener = new LocationListener() {
 public void onLocationChanged(Location location) {
  updateWithNewLocation(location);
}

public void onProviderDisabled(String provider){
  updateWithNewLocation(null);
}

public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status, 
                            Bundle extras){ }
 };

private void updateWithNewLocation(Location location) {
String latLongString ="";
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
String addressString = "No address found";

if (location != null) {
     // Update my location marker
    positionOverlay.setLocation(location);

  // Update the map location.
  Double geoLat = location.getLatitude()*1E6;
  Double geoLng = location.getLongitude()*1E6;
  GeoPoint point = new GeoPoint(geoLat.intValue(), 
                                geoLng.intValue());

  mapController.animateTo(point);

  double lat = location.getLatitude();
  double lng = location.getLongitude();
  latLongString = "Lat:" + lat + "\nLong:" + lng;
  Global.lat = lat;
  Global.lng = lng;
  double latitude = location.getLatitude();
  double longitude = location.getLongitude();

  Geocoder gc = new Geocoder(this, Locale.getDefault());
  try {
    List<Address> addresses = gc.getFromLocation(latitude, 
                                                 longitude, 1);
    StringBuilder sb = new StringBuilder();
    if (addresses.size() > 0) {
      Address address = addresses.get(0);

      for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
        sb.append(address.getAddressLine(i)).append("\n");

        sb.append(address.getLocality()).append("\n");
        sb.append(address.getPostalCode()).append("\n");
        sb.append(address.getCountryName());
    }
    addressString = sb.toString();
  } catch (IOException e) {}
} else {
  latLongString = "No location found";
}
}

class MapOverlay extends com.google.android.maps.Overlay
{
  @Override
  public boolean draw(Canvas canvas, MapView mapView, 
  boolean shadow, long when) 
  {
      super.draw(canvas, mapView, shadow);                   

      //---translate the GeoPoint to screen pixels---
      Point screenPts = new Point();
      mapView.getProjection().toPixels(p, screenPts);

      //---add the marker---
      Bitmap bmp = BitmapFactory.decodeResource(
          getResources(), R.drawable.marker);            
      canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
      return true;
 } 

 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:32:03+00:00Added an answer on May 16, 2026 at 10:32 am

    I see a declaration of GeoPoint p but no expression where p is actually initialized. So passing null to the toPixels() method could be a cause for the NPE.

    Try this code instead to check, if the NPE is gone:

      //---translate the GeoPoint to screen pixels---
      if (p == null) return false;
      Point screenPts = mapView.getProjection().toPixels(p, null);
    

    (I don’t like ‘out’ parameters in Java, toPixels will create the point object for you
    and return it)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.