Reputation: 3728
I have the following code on a Windows machine:
for(Enumeration enm = NetworkInterface.getNetworkInterfaces(); enm.hasMoreElements();){
NetworkInterface network = (NetworkInterface) enm.nextElement();
if(null != network.getHardwareAddress()){
return EthernetAddress.valueOf(network.getHardwareAddress());
}
}
This fails because the network.getHardwareAddress() returns an empty byte array, instead of null as stated in the javadocs for NetworkInterface. Does anyone know why this may happen?
Upvotes: 0
Views: 4362
Reputation: 350
The byte array returned from network.getHardwareAddress() gets null if you are not connected to any network (LAN or WAN) hence it returns null value
Upvotes: 0
Reputation: 533492
I did this and saw that the loopback (which apeared first) has no mac address.
My guess is that null
is intended for when the OS returns that the MAC was unavailable. However it may have returned empty data instead.
On Linux, it doesn't show me the loopback.
Upvotes: 2