diff --git a/src/org/openstreetmap/josm/actions/AlignGridRotationAction.java b/src/org/openstreetmap/josm/actions/AlignGridRotationAction.java
index 5f7459a839..8062902e44 100644
--- a/src/org/openstreetmap/josm/actions/AlignGridRotationAction.java
+++ b/src/org/openstreetmap/josm/actions/AlignGridRotationAction.java
@@ -26,6 +26,9 @@ import org.openstreetmap.josm.gui.layer.MapGridPaintable.GridType;
  */
 public class AlignGridRotationAction extends JosmAction {
 
+    /** returned when the selection does not define a direction */
+    private static final EastNorth[] EMPTY_DIRECTION = new EastNorth[0];
+
     /**
      * Constructs a new {@code AlignGridRotationAction}.
      */
@@ -39,11 +42,11 @@ public class AlignGridRotationAction extends JosmAction {
     /**
      * Determines the direction given by the selection: a single way with at least two nodes, or exactly two nodes.
      * @param ds the data set, may be null
-     * @return start and end point, or {@code null} if the selection does not define a direction
+     * @return start and end point, or an empty array if the selection does not define a direction
      */
     static EastNorth[] getSelectedDirection(DataSet ds) {
         if (ds == null) {
-            return null;
+            return EMPTY_DIRECTION;
         }
         Collection<Way> ways = ds.getSelectedWays();
         Collection<Node> nodes = ds.getSelectedNodes();
@@ -61,10 +64,10 @@ public class AlignGridRotationAction extends JosmAction {
             b = it.next();
         }
         if (a == null || b == null || !a.isLatLonKnown() || !b.isLatLonKnown()) {
-            return null;
+            return EMPTY_DIRECTION;
         }
         EastNorth[] result = {a.getEastNorth(), b.getEastNorth()};
-        return result[0].equalsEpsilon(result[1], 1e-9) ? null : result;
+        return result[0].equalsEpsilon(result[1], 1e-9) ? EMPTY_DIRECTION : result;
     }
 
     /**
@@ -85,7 +88,7 @@ public class AlignGridRotationAction extends JosmAction {
 
     @Override
     protected void updateEnabledState() {
-        setEnabled(getSelectedDirection(getLayerManager().getEditDataSet()) != null);
+        setEnabled(getSelectedDirection(getLayerManager().getEditDataSet()).length == 2);
     }
 
     @Override
@@ -96,7 +99,7 @@ public class AlignGridRotationAction extends JosmAction {
     @Override
     public void actionPerformed(ActionEvent e) {
         EastNorth[] direction = getSelectedDirection(getLayerManager().getEditDataSet());
-        if (direction == null) {
+        if (direction.length != 2) {
             return;
         }
         MapGridPaintable.ROTATION.put(rotationOf(direction[0], direction[1]));
diff --git a/src/org/openstreetmap/josm/gui/MapFrame.java b/src/org/openstreetmap/josm/gui/MapFrame.java
index 341371d0c4..a9864246c5 100644
--- a/src/org/openstreetmap/josm/gui/MapFrame.java
+++ b/src/org/openstreetmap/josm/gui/MapFrame.java
@@ -124,7 +124,7 @@ public class MapFrame extends JPanel implements Destroyable, ActiveLayerChangeLi
      */
     public final MapView mapView;
     /** The grid drawn over the map view, see {@link MapGridPaintable} */
-    private final MapGridPaintable gridOverlay = new MapGridPaintable();
+    private final transient MapGridPaintable gridOverlay = new MapGridPaintable();
 
     /**
      * This object allows to detect key press and release events
diff --git a/src/org/openstreetmap/josm/gui/layer/MapGridPaintable.java b/src/org/openstreetmap/josm/gui/layer/MapGridPaintable.java
index f6fc491529..0a5bc43691 100644
--- a/src/org/openstreetmap/josm/gui/layer/MapGridPaintable.java
+++ b/src/org/openstreetmap/josm/gui/layer/MapGridPaintable.java
@@ -12,11 +12,11 @@ import java.awt.geom.Path2D;
 import java.awt.geom.Point2D;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
 
 import org.openstreetmap.josm.data.Bounds;
 import org.openstreetmap.josm.data.ProjectionBounds;
 import org.openstreetmap.josm.data.coor.EastNorth;
-import org.openstreetmap.josm.data.coor.ILatLon;
 import org.openstreetmap.josm.data.coor.LatLon;
 import org.openstreetmap.josm.data.preferences.BooleanProperty;
 import org.openstreetmap.josm.data.preferences.DoubleProperty;
@@ -137,7 +137,7 @@ public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer
     private static final int MAX_LINES = 500;
 
     /** written by {@link #paint} and read by {@link #preferenceChanged}, which may run on another thread */
-    private volatile MapView mapView;
+    private final AtomicReference<MapView> mapView = new AtomicReference<>();
 
     /**
      * Constructs a new {@code MapGridPaintable}.
@@ -148,7 +148,7 @@ public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer
 
     @Override
     public void paint(Graphics2D g, MapView mv, Bounds bbox) {
-        mapView = mv;
+        mapView.set(mv);
         if (!Boolean.TRUE.equals(ENABLED.get()) || mv.getWidth() <= 0 || mv.getHeight() <= 0) {
             return;
         }
@@ -318,7 +318,7 @@ public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer
             if (!a.isValid() || !b.isValid()) {
                 return 1;
             }
-            double metres = a.greatCircleDistance((ILatLon) b);
+            double metres = a.greatCircleDistance(b);
             return metres > 0 && Double.isFinite(metres) ? step / metres : 1;
         } catch (IllegalArgumentException e) {
             Logging.trace(e);
@@ -357,7 +357,7 @@ public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer
     public static List<ProjectedGridLine> getProjectedGridLines(ProjectionBounds area, double spacingX, double spacingY,
             double rotationDegrees, double originX, double originY) {
         List<ProjectedGridLine> lines = new ArrayList<>();
-        if (!(spacingX > 0) || !(spacingY > 0)) {
+        if (!isPositive(spacingX) || !isPositive(spacingY)) {
             return lines;
         }
         double angle = Math.toRadians(rotationDegrees);
@@ -420,7 +420,7 @@ public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer
     public static List<LatLonGridLine> getLatLonGridLines(Bounds area, Bounds world, double spacingLon, double spacingLat,
             double originLon, double originLat, int segments) {
         List<LatLonGridLine> lines = new ArrayList<>();
-        if (!(spacingLon > 0) || !(spacingLat > 0) || segments < 1) {
+        if (!isPositive(spacingLon) || !isPositive(spacingLat) || segments < 1) {
             return lines;
         }
         double minLat = Math.max(area.getMinLat(), world.getMinLat());
@@ -445,14 +445,9 @@ public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer
         boolean limitedLon = world.getMinLon() > -180 || world.getMaxLon() < 180;
         for (long k = kLonMin; k <= kLonMax; k++) {
             double lon = LatLon.toIntervalLon(originLon + k * spacingLon);
-            if (limitedLon && (lon < world.getMinLon() || lon > world.getMaxLon())) {
-                continue;
-            }
-            List<LatLon> line = new ArrayList<>(segments + 1);
-            for (int i = 0; i <= segments; i++) {
-                line.add(new LatLon(minLat + (maxLat - minLat) * i / segments, lon));
+            if (!limitedLon || (lon >= world.getMinLon() && lon <= world.getMaxLon())) {
+                addMeridian(lines, lon, minLat, maxLat, segments);
             }
-            lines.add(new LatLonGridLine(line, true));
         }
         for (long k = kLatMin; k <= kLatMax; k++) {
             double lat = originLat + k * spacingLat;
@@ -464,6 +459,22 @@ public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer
         return lines;
     }
 
+    /**
+     * Adds a meridian running from one latitude to another.
+     * @param lines the list to add to
+     * @param lon the longitude of the meridian
+     * @param minLat the latitude to start at
+     * @param maxLat the latitude to end at
+     * @param segments number of segments of the polyline
+     */
+    private static void addMeridian(List<LatLonGridLine> lines, double lon, double minLat, double maxLat, int segments) {
+        List<LatLon> line = new ArrayList<>(segments + 1);
+        for (int i = 0; i <= segments; i++) {
+            line.add(new LatLon(minLat + (maxLat - minLat) * i / segments, lon));
+        }
+        lines.add(new LatLonGridLine(line, true));
+    }
+
     /**
      * Adds a parallel running from one longitude to another. A parallel which crosses the antimeridian is added
      * as two lines, one on each side of it: wrapping the longitudes of a single polyline would instead make it
@@ -480,7 +491,7 @@ public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer
             addParallel(lines, lat, -180, maxLon - 360, segments);
             return;
         }
-        if (!(maxLon > minLon)) {
+        if (maxLon <= minLon) {
             return;
         }
         List<LatLon> line = new ArrayList<>(segments + 1);
@@ -490,12 +501,22 @@ public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer
         lines.add(new LatLonGridLine(line, false));
     }
 
+    /**
+     * Determines if a value is strictly positive. Unlike {@code value <= 0}, this rejects NaN.
+     * @param value the value
+     * @return {@code true} if the value is greater than 0
+     */
+    private static boolean isPositive(double value) {
+        return value > 0;
+    }
+
     @Override
     public void preferenceChanged(PreferenceChangeEvent e) {
         if (e.getKey().startsWith(PREFIX) || e.getKey().equals(COLOR_EAST.getKey()) || e.getKey().equals(COLOR_NORTH.getKey())) {
             invalidate();
-            if (mapView != null) {
-                mapView.repaint();
+            MapView mv = mapView.get();
+            if (mv != null) {
+                mv.repaint();
             }
         }
     }
@@ -503,6 +524,6 @@ public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer
     @Override
     public void destroy() {
         Config.getPref().removePreferenceChangeListener(this);
-        mapView = null;
+        mapView.set(null);
     }
 }
diff --git a/src/org/openstreetmap/josm/gui/layer/imagery/ShowTileBordersAction.java b/src/org/openstreetmap/josm/gui/layer/imagery/ShowTileBordersAction.java
index 8c2cf5ecbe..8a53b22978 100644
--- a/src/org/openstreetmap/josm/gui/layer/imagery/ShowTileBordersAction.java
+++ b/src/org/openstreetmap/josm/gui/layer/imagery/ShowTileBordersAction.java
@@ -20,7 +20,7 @@ import org.openstreetmap.josm.gui.layer.Layer.LayerAction;
  */
 public class ShowTileBordersAction extends AbstractAction implements LayerAction {
 
-    private final AbstractTileSourceLayer<?> layer;
+    private final transient AbstractTileSourceLayer<?> layer;
 
     /**
      * Constructs a new {@code ShowTileBordersAction}.
diff --git a/src/org/openstreetmap/josm/gui/preferences/display/GridPreference.java b/src/org/openstreetmap/josm/gui/preferences/display/GridPreference.java
index 5899b72d03..1af89faf58 100644
--- a/src/org/openstreetmap/josm/gui/preferences/display/GridPreference.java
+++ b/src/org/openstreetmap/josm/gui/preferences/display/GridPreference.java
@@ -4,6 +4,7 @@ package org.openstreetmap.josm.gui.preferences.display;
 import static org.openstreetmap.josm.tools.I18n.tr;
 
 import java.awt.Component;
+import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
@@ -100,7 +101,7 @@ public class GridPreference extends DefaultTabPreferenceSetting {
 
         panel.add(enabled, GBC.eol().insets(0, 0, 0, 10));
         panel.add(new JLabel(tr("Grid type")), GBC.std().insets(5, 0, 5, 5));
-        panel.add(type, GBC.eol().fill(GBC.HORIZONTAL).insets(0, 0, 0, 5));
+        panel.add(type, GBC.eol().fill(GridBagConstraints.HORIZONTAL).insets(0, 0, 0, 5));
         panel.add(spacingXLabel, GBC.std().insets(5, 0, 5, 5));
         panel.add(spacingX, GBC.eol().insets(0, 0, 0, 5));
         panel.add(spacingYLabel, GBC.std().insets(5, 0, 5, 5));
@@ -116,8 +117,8 @@ public class GridPreference extends DefaultTabPreferenceSetting {
         panel.add(new JLabel(tr("Line width (pixels)")), GBC.std().insets(5, 0, 5, 5));
         panel.add(lineWidth, GBC.eol().insets(0, 0, 0, 5));
         panel.add(new JLabel(tr("Blend mode")), GBC.std().insets(5, 0, 5, 5));
-        panel.add(blendMode, GBC.eol().fill(GBC.HORIZONTAL).insets(0, 0, 0, 5));
-        panel.add(GBC.glue(0, 0), GBC.eol().fill(GBC.BOTH));
+        panel.add(blendMode, GBC.eol().fill(GridBagConstraints.HORIZONTAL).insets(0, 0, 0, 5));
+        panel.add(GBC.glue(0, 0), GBC.eol().fill(GridBagConstraints.BOTH));
 
         createPreferenceTabWithScrollPane(gui, panel);
     }
@@ -142,7 +143,7 @@ public class GridPreference extends DefaultTabPreferenceSetting {
 
     /** Renders enum values with a translated name */
     private static final class TranslatedRenderer<T> extends DefaultListCellRenderer {
-        private final Function<T, String> name;
+        private final transient Function<T, String> name;
 
         TranslatedRenderer(Function<T, String> name) {
             this.name = name;
@@ -178,7 +179,7 @@ public class GridPreference extends DefaultTabPreferenceSetting {
     private static void save(JosmTextField field, DoubleProperty property, boolean positive) {
         try {
             double value = FORMAT.parse(field.getText().trim()).doubleValue();
-            if (positive && !(value > 0)) {
+            if (positive && (Double.isNaN(value) || value <= 0)) {
                 Logging.warn("Ignoring invalid grid setting {0}: {1}", property.getKey(), field.getText());
                 return;
             }
diff --git a/test/unit/org/openstreetmap/josm/actions/GridActionsTest.java b/test/unit/org/openstreetmap/josm/actions/GridActionsTest.java
index 37e9d667c6..5d59504846 100644
--- a/test/unit/org/openstreetmap/josm/actions/GridActionsTest.java
+++ b/test/unit/org/openstreetmap/josm/actions/GridActionsTest.java
@@ -164,14 +164,14 @@ class GridActionsTest {
             ds.addPrimitive(w);
 
             ds.setSelected();
-            assertNull(AlignGridRotationAction.getSelectedDirection(ds));
+            assertEquals(0, AlignGridRotationAction.getSelectedDirection(ds).length);
             AlignGridRotationAction action = new AlignGridRotationAction();
             assertFalse(action.isEnabled());
             ds.setSelected(a);
-            assertNull(AlignGridRotationAction.getSelectedDirection(ds));
+            assertEquals(0, AlignGridRotationAction.getSelectedDirection(ds).length);
             assertFalse(action.isEnabled());
             ds.setSelected(a, b, c);
-            assertNull(AlignGridRotationAction.getSelectedDirection(ds));
+            assertEquals(0, AlignGridRotationAction.getSelectedDirection(ds).length);
             assertFalse(action.isEnabled());
 
             ds.setSelected(a, b);
diff --git a/test/unit/org/openstreetmap/josm/io/OsmConnectionTest.java b/test/unit/org/openstreetmap/josm/io/OsmConnectionTest.java
index f742a38422..61fb9b5c11 100644
--- a/test/unit/org/openstreetmap/josm/io/OsmConnectionTest.java
+++ b/test/unit/org/openstreetmap/josm/io/OsmConnectionTest.java
@@ -8,7 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.net.MalformedURLException;
-import java.net.URL;
+import java.net.URI;
 
 import javax.swing.JOptionPane;
 
@@ -59,7 +59,7 @@ class OsmConnectionTest {
     }
 
     private static HttpClient apiRequest() throws MalformedURLException {
-        return HttpClient.create(new URL(API_URL + "/0.6/changeset/create"), "PUT");
+        return HttpClient.create(URI.create(API_URL + "/0.6/changeset/create").toURL(), "PUT");
     }
 
     /**
