Skip to content
Advertisement

Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor

I have two java classes Configuration.java and 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";

can anyone help me please to resolve the error

Advertisement

Answer

   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 ...

   }  

Also change your Configuration file like this:

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

        }
    }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement