[ 
https://issues.apache.org/jira/browse/LANG-1472?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16906019#comment-16906019
 ] 

Silence Tai commented on LANG-1472:
-----------------------------------

[~VioletKiss]

The *getHostName* method works on the non-Widnwos platform depending on the 
*HOSTNAME* environment variable, but there is no definition in your current 
environment variable, so getting {color:#FF0000}null{color} is normal.
{code:java}
    /**
     * Gets the host name from an environment variable.
     *
     * <p>
     * If you want to know what the network stack says is the host name, you 
should use {@code InetAddress.getLocalHost().getHostName()}.
     * </p>
     *
     * @return the host name.
     * @since 3.6
     */
    public static String getHostName() {
        return IS_OS_WINDOWS ? System.getenv("COMPUTERNAME") : 
System.getenv("HOSTNAME");
    }
{code}
You can see in the program through the *System.getenv()* method that the 
environment name of the *HOSTNAME* is not registered in the program instance.

> SystemUtils.getHostName()获取系统hostname为null
> ------------------------------------------
>
>                 Key: LANG-1472
>                 URL: https://issues.apache.org/jira/browse/LANG-1472
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.*
>    Affects Versions: 3.9
>         Environment: * 操作系统:阿里云提供的Ubuntu 16.04
> root@iZwz99ibpdra8w3j3jcbw7Z:~# hostname
> iZwz99ibpdra8w3j3jcbw7Z
> root@iZwz99ibpdra8w3j3jcbw7Z:~# echo $HOSTNAME
> iZwz99ibpdra8w3j3jcbw7Z
>  * 语言:Java 8
> root@iZwz99ibpdra8w3j3jcbw7Z:~# java -version
> java version "1.8.0_191"
> Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
> Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
>            Reporter: wangrubao
>            Priority: Critical
>         Attachments: SnowflakeIdWorker.java, error.png, pom.xml
>
>
> SpringBoot 项目获取系统 hostname 为 null 导致 java.lang.NullPointerException 。
> 部分代码:
>    private static Long getDataCenterId(){
>         int[] ints = StringUtils.toCodePoints(SystemUtils.getHostName());
>         int sums = 0;
>         for (int i: ints) {
>             sums += i;
>         }
>         return (long)(sums % 32);
>     }



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)

Reply via email to