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]

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
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());
}
}