8 Replies Latest reply: Jan 4, 2005 10:48 AM by 807596 RSS

    Nullpointer exception

    807596
      Hi, i am getting null pointer exception, below is the my 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());
      
        }
      
      }
      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();
      ^
        • 1. Re: Nullpointer exception
          807596
          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
          • 2. Re: Nullpointer exception
            807596
            InetAddress Inet; //= new InetAddress();
            Inet is null and you are calling a method on it. That's certainly NullPointerException.

            ***Annie***
            • 3. Re: Nullpointer exception
              807596
              Thanks for the response. Could you please tell me what modifications should go in that program.
              • 4. Re: Nullpointer exception
                807596
                The InetAddress constructor is not public. You have to get an instance of it by using
                InetAddress.getByName()
                or
                InetAddress.getByAddress()
                Look in the API docs.

                ***Annie***
                • 5. Re: Nullpointer exception
                  807596
                  hi, i am new to this stuff, wud you mind just correcting this program and send me back. Thanks for the help.
                  • 6. Re: Nullpointer exception
                    807596
                    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());
                     
                      }
                     
                    }
                    • 7. Re: Nullpointer exception
                      807596
                      Hi Annie,

                      Thanks a lot for your help. It worked. Great Thanks!!!!

                      -toLearn
                      • 8. Re: Nullpointer exception
                        807596
                        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.