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]
# 1
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 > top of Java-index,Core,Core APIs...
# 2
thanks for the reply. sorry if it sounds simple, but i've had little experience programming in java. my question is this: how do i assign a value to a NetworkInterface object? thanks
Philip_Ga at 2007-7-14 1:20:29 > top of Java-index,Core,Core APIs...
# 3
See the Javadoc. From memory you have to enumerate them.
ejpa at 2007-7-14 1:20:29 > top of Java-index,Core,Core APIs...
# 4

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(); }

yana_indraa at 2007-7-14 1:20:29 > top of Java-index,Core,Core APIs...