Browse Source

fix mac null bug

Looly 5 years ago
parent
commit
9d8f4d725c
2 changed files with 7 additions and 2 deletions
  1. 1 0
      CHANGELOG.md
  2. 6 2
      hutool-core/src/main/java/cn/hutool/core/net/NetUtil.java

+ 1 - 0
CHANGELOG.md

@@ -13,6 +13,7 @@
 * 【extra 】     新增方便引入SpringUtil的注解@EnableSpringUtil(pr#172@Gitee)
 * 【poi   】     RowUtil增加插入和删除行(pr#1060@Github)
 * 【extra 】     SpringUtil增加注册bean(pr#174@Gitee)
+* 【core  】     修改NetUtil.getMacAddress避免空指针(issue#1057@Github)
 
 ### Bug修复#
 * 【core  】     重新整理农历节假日,解决一个pr过来的玩笑导致的问题

+ 6 - 2
hutool-core/src/main/java/cn/hutool/core/net/NetUtil.java

@@ -507,9 +507,12 @@ public class NetUtil {
 			return null;
 		}
 
-		byte[] mac;
+		byte[] mac = null;
 		try {
-			mac = NetworkInterface.getByInetAddress(inetAddress).getHardwareAddress();
+			final NetworkInterface networkInterface = NetworkInterface.getByInetAddress(inetAddress);
+			if(null != networkInterface){
+				mac = networkInterface.getHardwareAddress();
+			}
 		} catch (SocketException e) {
 			throw new UtilException(e);
 		}
@@ -526,6 +529,7 @@ public class NetUtil {
 			}
 			return sb.toString();
 		}
+
 		return null;
 	}