public InetAddress[] getAllIp()
{
try {
InetAddress adr=InetAddress.getLocalHost();
String name=adr.getHostName();
InetAddress [] ip=InetAddress.getAllByName(name);
return ip;

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

}
 编译通不过,大家帮我找找原因吧,出错原因说是返回值的问题!

解决方案 »

  1.   

    public InetAddress[] getAllIp() 

    InetAddress[] ip = null;
    try { 
    InetAddress adr=InetAddress.getLocalHost(); 
    String name=adr.getHostName(); 
    ip=InetAddress.getAllByName(name); //因为这里是在try内,如果发生异常就没有返回值了,所以编译不能通过
    } catch (UnknownHostException e) { 
    e.printStackTrace(); 

    return ip;

      

  2.   


            public InetAddress[] getAllIp() {
    InetAddress[] ip=null;
    try {
    InetAddress adr = InetAddress.getLocalHost();
    String name = adr.getHostName();
    ip = InetAddress.getAllByName(name);
    } catch (UnknownHostException e) {
    e.printStackTrace();
    }
    return ip; }
      

  3.   

    public InetAddress[] getAllIp() 

    InetAddress[] ip = null;
    try { 
    InetAddress adr=InetAddress.getLocalHost(); 
    String name=adr.getHostName(); 
    ip=InetAddress.getAllByName(name); //因为这里是在try内,如果发生异常就没有返回值了,所以编译不能通过
    } catch (UnknownHostException e) { 
    e.printStackTrace(); 

    return ip;

      

  4.   

    你的return在try里面, 如果在try里面出错了它return什么呢?
    这样就可以了.public InetAddress[] getAllIp() {
    InetAddress[] ip = null;
    try {
    InetAddress adr = InetAddress.getLocalHost();
    String name = adr.getHostName();
    ip = InetAddress.getAllByName(name);
    } catch (UnknownHostException e) {
    e.printStackTrace();
    return null;
    }
    return ip;

      

  5.   

    public InetAddress[] getAllIp() 

    try { 
    InetAddress adr=InetAddress.getLocalHost(); 
    String name=adr.getHostName(); 
    InetAddress [] ip=InetAddress.getAllByName(name); 
    return ip; } catch (UnknownHostException e) { 
    e.printStackTrace(); 
    InetAddress[] ip=new InetAddress[0];
    return ip; 

        } 
      

  6.   

    呵呵 各位好快啊。
    果然是我想的那样,return放里面了。这错误也犯过。
      

  7.   

    InetAddress[] ip 不初始化为null可以吗,如果不初始化的话不是默认给他赋值null吗?为什么这个地方一点要初始化?
      

  8.   

    这样的代码,IDE就会有提示了,更别说编译了
      

  9.   

    package csdn;import java.net.InetAddress;
    import java.net.UnknownHostException;public class InetAddressTest { public InetAddress[] getAllIp() {
    InetAddress[] ip = null;
    try {
    InetAddress adr = InetAddress.getLocalHost();
    String name = adr.getHostName();
    ip = InetAddress.getAllByName(name);
    } catch (UnknownHostException e) {
    e.printStackTrace();
    }
    return ip; } public static void main(String[] args) {
    // TODO Auto-generated method stub
    InetAddressTest inet = new InetAddressTest();
    InetAddress[] ip = inet.getAllIp();
    for (InetAddress i : ip) {
    System.out.println(i); } }}
    -------------------------------------------------------------------------------------
    如果返回值类型不是void时,需要保证有值返回,例如下面的方法就有语法错误:
               public int test(int a){
                        if(a < 0){
                                 return 0;
                        }
               }
    则该方法的声明代码中,当a的值大于等于零时,则没有返回值,这语法上称作返回值丢失,这个也是在书写return语句时需要特别注意的问题。
    ----------------------------------------------------------------------------------------
    你犯的错误就上面的错误