Nullpointer exception

Hi, i am getting null pointer exception, below is the my code.

import java.net.*;

publicclass ConstructDefaultRequestInfo{

String _ipAddress =null;

String _subnetAddress =null;

String _hostName =null;

InetAddress Inet=new InetAddress();

public ConstructDefaultRequestInfo(){

}

String getSystemIpAddress()

{

try{

_ipAddress = Inet.getHostAddress();

_hostName = Inet.getHostName();

}catch(NullPointerException e){e.printStackTrace();}

return(_ipAddress);

}

publicstaticvoid main(String[] args){

ConstructDefaultRequestInfo constructDefaultRequestInfo1 =new ConstructDefaultRequestInfo();

System.out.println("MY SYSTEM IP is " + constructDefaultRequestInfo1.getSystemIpAddress());

}

}

code gets complied successfully but when i run, following excpetion will be returned. what is the problem and what needs to be corrected.

Error : ConstructDefaultRequestInfo.java:8: InetAddress() is not public in java.net.Inet

Address; cannot be accessed from outside package

InetAddress Inet= new InetAddress();

^

[2031 byte] By [toLearna] at [2007-10-1 1:44:58]
# 1

I made mistake in the previous code, here is the compiled code

import java.net.*;

public class ConstructDefaultRequestInfo {

String _ipAddress = null;

String _subnetAddress = null;

String _hostName = null;

InetAddress Inet; //= new InetAddress();

public ConstructDefaultRequestInfo() {

}

String getSystemIpAddress()

{

try{

_ipAddress = Inet.getHostAddress();

_hostName = Inet.getHostName();

} catch(NullPointerException e){e.printStackTrace();}

return(_ipAddress);

}

public static void main(String[] args) {

ConstructDefaultRequestInfo constructDefaultRequestInfo1 = new ConstructDefaultRequestInfo();

System.out.println("MY SYSTEM IP is " + constructDefaultRequestInfo1.getSystemIpAddress());

}

}

Exception: java.lang.NullPointerException

at ConstructDefaultRequestInfo.getSystemIpAddress(ConstructDefaultReques

tInfo.java:17)

at ConstructDefaultRequestInfo.main(ConstructDefaultRequestInfo.java:30)

MY SYSTEM IP is null

toLearna at 2007-7-8 8:07:30 > top of Java-index,Security,Event Handling...
# 2
>InetAddress Inet; //= new InetAddress();Inet is null and you are calling a method on it. That's certainly NullPointerException.***Annie***
annie79a at 2007-7-8 8:07:30 > top of Java-index,Security,Event Handling...
# 3
Thanks for the response. Could you please tell me what modifications should go in that program.
toLearna at 2007-7-8 8:07:30 > top of Java-index,Security,Event Handling...
# 4
The InetAddress constructor is not public. You have to get an instance of it by usingInetAddress.getByName()orInetAddress.getByAddress()Look in the API docs.***Annie***
annie79a at 2007-7-8 8:07:30 > top of Java-index,Security,Event Handling...
# 5
hi, i am new to this stuff, wud you mind just correcting this program and send me back. Thanks for the help.
toLearna at 2007-7-8 8:07:30 > top of Java-index,Security,Event Handling...
# 6

import java.net.*;

public class ConstructDefaultRequestInfo {

String _ipAddress = null;

String _subnetAddress = null;

String _hostName = null;

InetAddress Inet = null;

public ConstructDefaultRequestInfo() {

try {

Inet = InetAddress.getByName("<<Address name here...>>");

// Alternatively,

// Inet = InetAddress.getByAddress(byte[] addr);

// if you have a raw IP address.

// OR

// Inet = InetAddress.getLocalHost();

// For getting localhost.

} catch (Exception e) {

e.printStackTrace();

}

}

String getSystemIpAddress()

{

try{

_ipAddress = Inet.getHostAddress();

_hostName = Inet.getHostName();

} catch(NullPointerException e){e.printStackTrace();}

return(_ipAddress);

}

public static void main(String[] args) {

ConstructDefaultRequestInfo constructDefaultRequestInfo1 = new ConstructDefaultRequestInfo();

System.out.println("MY SYSTEM IP is " + constructDefaultRequestInfo1.getSystemIpAddress());

}

}

annie79a at 2007-7-8 8:07:30 > top of Java-index,Security,Event Handling...
# 7
Hi Annie,Thanks a lot for your help. It worked. Great Thanks!!!!-toLearn
toLearna at 2007-7-8 8:07:30 > top of Java-index,Security,Event Handling...
# 8
> hi, i am new to this stuff, wud you mind just> correcting this program and send me back. Thanks for> the help.Great way to learn.
yawmarka at 2007-7-8 8:07:30 > top of Java-index,Security,Event Handling...