Search code examples
javaaemapache-felix

cannot convert from String to Integer


I have implemented some service which works fine. The service has the following property:

 @Property(name = MyService.PROXY_PORT, label = "Proxy Port", description = "Proxy Port", value= "8080")
    private static final String PROXY_PORT = "proxy.port";

I get the value of the port property as follow:

..
...
properties = context.getProperties();
String port = properties.get(PROXY_PORT).toString();
...

port in this case results 8080. Now I would like to convert this String to Integer. I tried the following:

int portInt = Integer.parseInt(port);
int portInt2 = Integer.getInteger(port);

Both results null :(

What did I do worng?


Solution

  • If the port has a value and is not null then try:

    int portInt = Integer.valueOf(port.trim());