question about java.net.NetworkInterface.getHardwareAddress()
Hi,
I'm trying to get the physical by using java.net.NetworkInterface.getHardwareAddress(). However, it returns null every time.
I have something like this:
byte[] mac;
NetworkInterface net;
mac = net.getHardwareAddress();
How can I solve this problem? thanks
Message was edited by:
Philip_G
[381 byte] By [
Philip_Ga] at [2007-10-2 22:03:58]

It returns null under the conditions described in the Javadoc, which you may need to address if possible, but the code you've showed will just throw a NullPointerException unless you assign a value to 'net'.
ejpa at 2007-7-14 1:20:29 >

Yup, like ejp said you have to enumerate them, so the code should be something like this
try {
java.util.Enumeration eth = java.net.NetworkInterface.getNetworkInterfaces();
while (eth.hasMoreElements()) {
java.net.NetworkInterface eth0 = (java.net.NetworkInterface) eth.nextElement();
byte mac [] = eth0.getHardwareAddress();
if (mac != null) {
// actions /
}
}
} catch (Exception e) { e.printStackTrace(); }