Index: applications/editors/josm/plugins/waypoint_search/src/org/openstreetmap/josm/plugins/waypointSearch/WaypointSearchPlugin.java
===================================================================
--- applications/editors/josm/plugins/waypoint_search/src/org/openstreetmap/josm/plugins/waypointSearch/WaypointSearchPlugin.java	(revision 28541)
+++ applications/editors/josm/plugins/waypoint_search/src/org/openstreetmap/josm/plugins/waypointSearch/WaypointSearchPlugin.java	(revision 28624)
@@ -1,11 +1,12 @@
 package org.openstreetmap.josm.plugins.waypointSearch;
 
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import org.openstreetmap.josm.gui.MapFrame;
+import org.openstreetmap.josm.gui.MapView;
+import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
+import org.openstreetmap.josm.gui.layer.Layer;
 import org.openstreetmap.josm.plugins.Plugin;
 import org.openstreetmap.josm.plugins.PluginInformation;
-import org.openstreetmap.josm.Main;
-import org.openstreetmap.josm.gui.MapView;
-import org.openstreetmap.josm.gui.layer.Layer;
-import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
-import static org.openstreetmap.josm.tools.I18n.tr;
 
 public class WaypointSearchPlugin extends Plugin implements LayerChangeListener  {
@@ -15,6 +16,6 @@
     * @param info  information about the plugin and its local installation    
     */
-    private Engine engine = new Engine();
-    private SelectWaypointDialog waypointDialog = new SelectWaypointDialog(tr("Waypoint search"), "ToolbarIcon", tr("Search after waypoint. Click and move the map view to the waypoint."), null, 100);
+    private final Engine engine = new Engine();
+    private SelectWaypointDialog waypointDialog;
     
     public WaypointSearchPlugin(PluginInformation info) {
@@ -23,62 +24,31 @@
     }
      
-    @Override
+	@Override
+	public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
+		if (newFrame != null) {
+			newFrame.addToggleDialog(waypointDialog = new SelectWaypointDialog(
+					tr("Waypoint search"), "ToolbarIcon", tr("Search after waypoint. Click and move the map view to the waypoint."), null, 100));
+		} else {
+			waypointDialog = null;
+		}
+	}
+
+	@Override
     public void activeLayerChange(Layer oldLayer, Layer newLayer) {
-        // TODO Auto-generated method stub
-        
     }
-
     
     @Override
     public void layerAdded(Layer newLayer) {
-        //add dialog
-        if (Main.map.getToggleDialog(SelectWaypointDialog.class)==null) {
-            Main.map.addToggleDialog(waypointDialog);
-        }
-        //update search
-        if (engine.gpxLayersExist()) {
+        // update search
+        if (waypointDialog != null && engine.gpxLayersExist()) {
             waypointDialog.updateSearchResults();
         }
     }
 
-
-
     @Override
     public void layerRemoved(Layer oldLayer) {
-        if (!engine.gpxLayersExist()) {
+        if (waypointDialog != null && !engine.gpxLayersExist()) {
             waypointDialog.updateSearchResults();
         }   
     }
- 
 }
-
-
-
-    
-    
-    
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-    
-    
-
-
-
-
-
-
