CrazyEngineers
  • Problem in getHostAddress() method in java

    Manish Goyal

    Member

    Updated: Oct 22, 2024
    Views: 1.0K
    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());}
    }
    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • bharathpb

    MemberJun 7, 2010

    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());}
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJun 7, 2010

    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 ?
    Are you sure? This action cannot be undone.
    Cancel
  • bharathpb

    MemberJun 7, 2010

    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...
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJun 8, 2010

    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?
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberJun 8, 2010

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJun 8, 2010

    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😕😕
    Are you sure? This action cannot be undone.
    Cancel
  • bharathpb

    MemberJun 8, 2010

    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????
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJun 8, 2010

    yes it simply output null in case of my first program
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register