默认构造函数无法处理隐式超级构造函数抛出的异常类型IOException。必须定义一个显式构造函数

时间:2015-03-04 12:13:03

标签: java android

我有两个java类Configuration.java和login.java

Configuration.java

public class Configuration {
    public Configuration() {}

    public String getparams() throws IOException {
        Properties properties = new Properties();
        FileInputStream fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
        try {
            properties.load(fileStream);
            String ip = (String) properties.get("IP");
            String port = (String) properties.get("Port");
            return ip + port;

        } finally {
            try {
                fileStream.close();
            } catch (IOException ex) {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}

login.java

...

Configuration cfg=new Configuration();
String ip=cfg.getparams();// error
String port=cfg.getparams();//error
private final String LOGIN_URL = "http://"+ ip +":"+port+"/webservice/login.php";
  

任何人都可以帮我解决错误

3 个答案:

答案 0 :(得分:0)

   public class Login extends Activity implements OnClickListener {

    Configuration cfg;
    String ip;
    String port;
    private String LOGIN_URL;
    //other variables

    protected void onCreate(Bundle savedInstanceState) {
        //here you initialize your variables
        try {
            cfg = new Configuration();
            String[] params = cfg.getparams();
            ip = params[0];
            port = params[1];
            LOGIN_URL = "http://"+ ip +":"+port+"/webservice/login.php";
        } catch (IOException e) {
            //handle the exception
        }
        //.. your other code ...
    }
    //... your other methods ...

   }  

同样更改配置文件:

public class Configuration {
    public Configuration() {}

    public String[] getparams() throws IOException {
        Properties properties = new Properties();
        FileInputStream fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
        try {
            properties.load(fileStream);
            String ip = (String) properties.get("IP");
            String port = (String) properties.get("Port");
            return new String[]{ip, port};

        } finally {
            try {
                fileStream.close();
            } catch (IOException ex) {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}

答案 1 :(得分:0)

为什么不将getParam的签名更改为不抛出错误。我看到你正在部分地调用properties.load();

类似的东西:

公共类配置{

public Configuration() {}

public String getparams() {
    Properties properties = new Properties();
    FileInputStream fileStream = null;
    try {

        fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");

        properties.load(fileStream);
        String ip = (String) properties.get("IP");
        String port = (String) properties.get("Port");
        return ip + port;

    } 
    catch(IOException ex)
    {
        return null;
    }
   finally {
        try {
            fileStream.close();
        } catch (IOException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }

    }
}

}

您可能希望将新属性移动到try块中,如果它也可以抛出。

答案 2 :(得分:0)

只需从 Configuration.java 中删除 throws IOException 并使用 try-catch 块处理异常。 throws 只声明可能发生的异常,不处理异常。

public class Configuration {
    public Configuration() {}

    public String getparams() {
        Properties properties = new Properties();
        FileInputStream fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
        try {
            properties.load(fileStream);
            String ip = (String) properties.get("IP");
            String port = (String) properties.get("Port");
            return ip + port;
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        finally {
            try {
                fileStream.close();
            } catch (IOException ex) {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}
相关问题