Index: src/org/openstreetmap/josm/data/osm/IWaySegment.java
===================================================================
--- src/org/openstreetmap/josm/data/osm/IWaySegment.java	(revision 19063)
+++ src/org/openstreetmap/josm/data/osm/IWaySegment.java	(working copy)
@@ -96,18 +96,18 @@
      * @param first first node
      * @param second second node
      * @return way segment
-     * @throws IllegalArgumentException if the node pair is not part of way
+     * @throws IllegalArgumentException if the node pair is a segment of the way
      */
     public static <N extends INode, W extends IWay<N>> IWaySegment<N, W> forNodePair(W way, N first, N second) {
         int endIndex = way.getNodesCount() - 1;
         while (endIndex > 0) {
             final int indexOfFirst = way.getNodes().subList(0, endIndex).lastIndexOf(first);
-            if (second.equals(way.getNode(indexOfFirst + 1))) {
+            if (indexOfFirst >= 0 && second.equals(way.getNode(indexOfFirst + 1))) {
                 return new IWaySegment<>(way, indexOfFirst);
             }
             endIndex--;
         }
-        throw new IllegalArgumentException("Node pair is not part of way!");
+        throw new IllegalArgumentException("Node pair is not a segment of the way!");
     }
 
     /**
Index: src/org/openstreetmap/josm/data/osm/WaySegment.java
===================================================================
--- src/org/openstreetmap/josm/data/osm/WaySegment.java	(revision 19063)
+++ src/org/openstreetmap/josm/data/osm/WaySegment.java	(working copy)
@@ -25,18 +25,18 @@
      * @param first  first node
      * @param second second node
      * @return way segment
-     * @throws IllegalArgumentException if the node pair is not part of way
+     * @throws IllegalArgumentException if the node pair is a segment of the way
      */
     public static WaySegment forNodePair(Way way, Node first, Node second) {
         int endIndex = way.getNodesCount() - 1;
         while (endIndex > 0) {
             final int indexOfFirst = way.getNodes().subList(0, endIndex).lastIndexOf(first);
-            if (second.equals(way.getNode(indexOfFirst + 1))) {
+            if (indexOfFirst >= 0 && second.equals(way.getNode(indexOfFirst + 1))) {
                 return new WaySegment(way, indexOfFirst);
             }
             endIndex--;
         }
-        throw new IllegalArgumentException("The node pair is not consecutive part of the way!");
+        throw new IllegalArgumentException("Node pair is not a segment of the way!");
     }
 
     /**
Index: test/unit/org/openstreetmap/josm/data/osm/WaySegmentTest.java
===================================================================
--- test/unit/org/openstreetmap/josm/data/osm/WaySegmentTest.java	(revision 19064)
+++ test/unit/org/openstreetmap/josm/data/osm/WaySegmentTest.java	(working copy)
@@ -36,6 +36,29 @@
         assertEquals(WaySegment.forNodePair(w, n1, n4).getLowerIndex(), 4);
         assertEquals(WaySegment.forNodePair(w, n4, n1).getLowerIndex(), 5);
         IllegalArgumentException iae = assertThrows(IllegalArgumentException.class, () -> WaySegment.forNodePair(w, n3, n4));
-        assertEquals("The node pair is not consecutive part of the way!", iae.getMessage());
+        assertEquals("Node pair is not a segment of the way!", iae.getMessage());
     }
+
+    @Test
+    void testForNodePair2() {
+        final DataSet ds = new DataSet();
+        final Node n1 = new Node(LatLon.ZERO);
+        final Node n2 = new Node(new LatLon(1, 0));
+        final Node n3 = new Node(new LatLon(2, 0));
+        final Node n4 = new Node(new LatLon(3, 0));
+        final Way w = new Way();
+        for (OsmPrimitive p : Arrays.asList(n1, n2, n3, n4, w)) {
+            ds.addPrimitive(p);
+        }
+        w.addNode(n1);
+        w.addNode(n2);
+        w.addNode(n3);
+        // wrong order
+        IllegalArgumentException iae = assertThrows(IllegalArgumentException.class, () -> WaySegment.forNodePair(w, n2, n1));
+        assertEquals("Node pair is not a segment of the way!", iae.getMessage());
+        // node is not in way
+        iae = assertThrows(IllegalArgumentException.class, () -> WaySegment.forNodePair(w, n1, n4));
+        assertEquals("Node pair is not a segment of the way!", iae.getMessage());
+    }
+
 }
