Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8175325

NetworkInterface.getInterfaceAddresses throws NPE when no addresses

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 9
    • 9
    • core-libs
    • b161
    • Verified

        (as reported by a colleague)

        When a NetworkInterface does not (yet) have any addresses, then getInterfaceAddresses should return an empty list, but it throws NullPointerException instead.

        public class EmptyNetworkInterface {
            public static void main(String[] args) throws Exception {
                // throws NPE
                new java.net.MulticastSocket()
                    .getNetworkInterface()
                    .getInterfaceAddresses();
            }
        }

        Exception in thread "main" java.lang.NullPointerException
        at java.base/java.net.NetworkInterface.getInterfaceAddresses(NetworkInterface.java:179)

        Colleague's suggested fix:
        @@ -90,6 +90,7 @@ public final class NetworkInterface {
                 this.name = name;
                 this.index = index;
                 this.addrs = addrs;
        + this.bindings = new InterfaceAddress[0];
             }

             /**

        (or add more null checks?)

              msheppar Mark Sheppard
              martin Martin Buchholz
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: