Problem in getHostAddress() method in java

I am trying to create a simple program that will return the address of host ie my computer

but it gives me exception null

I don't understand why?

here is my code

import java.net.*;
class Myserver
{
public static void main(String args[])
{
InetAddress is=null;
try
{
String st=is.getHostName();
System.out.println("Host address is "+st);
}
catch(Exception e)
{
System.out.println(e.getMessage());}
}
}

Replies

  • bharathpb
    bharathpb
    Not sure wats going wrong in ur code..

    InetAddress is=null; is setting the address to null...finally u get null.


    I tried a new code..try this one...

    import java.net.*;
    class Myserver1
    {
    public static void main(String args[])
    {

    try
    { InetAddress addr = InetAddress.getLocalHost();

    System.out.println("Host address is " +addr);
    } catch (UnknownHostException e) {
    System.out.println(e.getMessage());}
    }
    }
  • Manish Goyal
    Manish Goyal
    Thanks for your response

    but still i am not getting same exception

    one thing is it possible to retrieve ip address that is allocated to me by my isp through this method ?
  • bharathpb
    bharathpb
    hi Goyal,

    from my above code..you will get both the system hostname and ip address allotted to u.

    Also post the exact exception so that i can debug...
  • Manish Goyal
    Manish Goyal
    nothing it just print null on console

    I don't understand what is the problem ?

    should i need to change my network settings?

    Do you use broadband?if yes then you program gives which ip address ?I mean that which was allocated by isp or any other?
  • sookie
    sookie
    Hi goyal420,

    I am using broadband connection and if I run bharathpb's program in my machine, it is working fine. It shows my machine's [hostname]/[ipaddress provided by ISP] in the output. I think you should once check network settings in your machine.
  • Manish Goyal
    Manish Goyal
    Thanks Guys for your reply

    now i am getting my ip address

    but why my program is not working ?I don't think that there is any syntax error but still there is a exception😕😕
  • bharathpb
    bharathpb
    goyal,,

    As already mentioned by me..provide the exact exception which you are getting...is it null pointer exception or is your output simply null????
  • Manish Goyal
    Manish Goyal
    yes it simply output null in case of my first program

You are reading an archived discussion.

Related Posts

hello everyone... I need a schematic of rms (effective voltage) that is designed by opamps and transistors. I found a circuit in the name of "true rms detector" but i...
.. Whaling is rampant in waters of INDIAN oceans on the name of research. Each season app 2500 whales are killed on the most cruel manner. WSPA has launched a...
Officially announced! ( not the drunken act of the employee 😁) Iphone 4 promises 100 new features and as of now, looks too hot to handle! and OS4 can be...
I am bumping into the same error for a very long time. fid=fopen('abc.m') fid = -1 I store a m file(and a txt file in desktop and import it to...
Hi Friends, While doing the heat load for an area we find the ADP based on the ESHR and the Room inside condition form the Table no 65 of Carrier...