0

Hello I am trying to plot a array of lattitude and longitude on Maps using itemized overlay. I am getting the following error .what could be the problem..????

>

 03-18 23:52:06.834: W/dalvikvm(2949): threadid=1: thread exiting with uncaught exception (group=0x40b01428)
03-18 23:52:06.844: E/AndroidRuntime(2949): FATAL EXCEPTION: main
03-18 23:52:06.844: E/AndroidRuntime(2949): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rahul.besttracker/com.rahul.besttracker.Mapview}: java.lang.NullPointerException
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.ActivityThread.access$600(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.os.Handler.dispatchMessage(Handler.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.os.Looper.loop(Looper.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.ActivityThread.main(ActivityThread.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at java.lang.reflect.Method.invokeNative(Native Method)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at java.lang.reflect.Method.invoke(Method.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at dalvik.system.NativeStart.main(Native Method)
03-18 23:52:06.844: E/AndroidRuntime(2949): Caused by: java.lang.NullPointerException
03-18 23:52:06.844: E/AndroidRuntime(2949):     at com.google.android.maps.ItemizedOverlay.populate(ItemizedOverlay.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at com.rahul.besttracker.HelloItemizedOverlay.addOverlay(HelloItemizedOverlay.java:37)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at com.rahul.besttracker.Mapview.onCreate(Mapview.java:56)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.Activity.performCreate(Activity.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
03-18 23:52:06.844: E/AndroidRuntime(2949):     ... 12 more
03-18 23:52:07.935: D/Process(2949): killProcess, pid=2949

Here is my HelloItemizedOverlay Class

package com.rahul.besttracker;

    import java.util.ArrayList;

    import android.app.AlertDialog;
    import android.content.Context;
    import android.graphics.drawable.Drawable;

     import com.google.android.maps.ItemizedOverlay;
    import com.google.android.maps.OverlayItem;

    public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;

public HelloItemizedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    // TODO Auto-generated constructor stub
}

public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    mContext = context;
}

@Override
protected OverlayItem createItem(int i) {
    return mOverlays.get(i);
}

@Override
public int size() {
    return mOverlays.size();
}

public void addOverlay(OverlayItem overlay) {

    mOverlays.add(overlay);
    populate();   <--------------GETTING ERROR HERE
}

@Override
protected boolean onTap(int index) {
    OverlayItem item = mOverlays.get(index);
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    dialog.setTitle(item.getTitle());
    dialog.setMessage(item.getSnippet());
    dialog.show();
    return true;
}
    }

ANd here is my MapView Class

package com.rahul.besttracker;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
 import com.google.android.maps.Overlay;
 import com.google.android.maps.OverlayItem;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

public class Mapview extends MapActivity {
MapView map;
ArrayList<String> mArrayList;
double[] Lat;
double[] Lon;

String stops[];
int latarr[], lonarr[];
OverlayItem overlayitem;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maps);

    map = (MapView) findViewById(R.id.Map);
    // map.setBuiltInZoomControls(true);
    // setContentView(R.layout.maps);
    Bundle gotBasket = getIntent().getExtras();
    mArrayList = gotBasket.getStringArrayList("stops");
    Lat = gotBasket.getDoubleArray("Lat");
    Lon = gotBasket.getDoubleArray("Lon");

    System.out.println("SIZE+++" + Lat.length + " " + Lon.length);
    for (int i = 0; i < mArrayList.size(); i++) {
        System.out.println(" \n" + Lat[i] + "\t" + Lon[i]);
    }
    // stops=(String[]) mArrayList.toArray();

    List<Overlay> mapOverlays = map.getOverlays();
    Drawable drawable = this.getResources().getDrawable(
            R.drawable.mark_blue);
    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
            drawable, this);

    for (int i = 0; i < mArrayList.size(); i++) {

        if (Lat[i] > 0 && Lon[i] > 0) {
            GeoPoint point = new GeoPoint((int) (Lat[i] * 1E6),
                    (int) (Lon[i] * 1E6));
            overlayitem = new OverlayItem(point, mArrayList.get(i), null);
        }
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);
    }
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}

Rahul Mehrotra
  • 639
  • 5
  • 15
  • 31

1 Answers1

0

In your for loop in onCreate, if you don't go into the if statement you won't have ever instantiated overlayitem, so you'd be adding a null pointer to the overlay. This would eventually cause the crash. The add should be part of the if.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • YOur for loop in onCreate is only setting a valid value for the variable overlayitem inside of an if statement, but you're using it after the if statement outside of it. If that condition is false, that means overlayitem is null. But you're still adding that as a marker to the map. That causes an eventual null pointer error. – Gabe Sechan Mar 18 '13 at 18:50