-2

I've got a some problem. My program cant't run, throws: nullPointException. On other IDE it's running.

One day ago I just click something, I don't know what that was :( Now this is look like this:

InetAddress localHost = Inet4Address.getLocalHost();

But InetAddress is strike throuh.

Please help, how can I undo this.

Karol Mks
  • 11
  • 2
  • 2
    These are two - and probably unrelated - questions (NPE is a popular question on SO), but you can forget about anyone helping you with the "I just click something, I don't know what that was" part. – kryger Dec 01 '17 at 11:43
  • See https://stackoverflow.com/a/44569788/104891 regarding the deprecated issue. – CrazyCoder Dec 01 '17 at 16:57

1 Answers1

0

This method is not deprecated in the version up to Java 9

https://docs.oracle.com/javase/9/docs/api/java/net/InetAddress.html#getLocalHost--

I have seen this fail on mis-configured machines. e.g. you have an invalid hosts file, though it shouldn't ever throw a NullPointerException AFAIK.

What I do is catch UnknownHostException and use "localhost" instead if this fails.

https://github.com/OpenHFT/Chronicle-Core/blob/master/src/main/java/net/openhft/chronicle/core/OS.java#L133

private static String getHostName0() {
    try {
        return InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        return "localhost";
    }
}
kryger
  • 12,906
  • 8
  • 44
  • 65
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130