I think there is a slight discrepancy between javadoc and the actual implementation of NetworkInterface getByInetAddress() . It seems that javadoc suggests that getByInetAddress returns null if no match was found, but the implementation either returns a match or throws a SocketException.
Javoc
public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException
Returns: NetworkInterface or null if there is no network interface with the specified IP address .
Implementation
public static NetworkInterface getByInetAddress (InetAddress addr) throws SocketException { if (networkInterfaces == null) networkInterfaces = getRealNetworkInterfaces (); for (Enumeration interfaces = networkInterfaces.elements (); interfaces.hasMoreElements (); ) { NetworkInterface tmp = (NetworkInterface) interfaces.nextElement (); for (Enumeration addresses = tmp.inetAddresses.elements (); addresses.hasMoreElements (); ) { if (addr.equals ((InetAddress) addresses.nextElement ())) return tmp; } } throw new SocketException ( "no network interface is bound to such an IP address"); }
I suggest either catch the exception, or consider it as a response from a third party, or getNetworkInterfaces() it using the getNetworkInterfaces() method.
source share