您的位置:首页 > 编程语言 > Java开发

java.lang.NumberFormatException: Invalid int: ""

2015-10-22 16:05 537 查看

java.lang.NumberFormatException: Invalid int: “”

http://stackoverflow.com/questions/24910757/java-lang-numberformatexception-invalid-int-null

错误代码

hostText = (EditText) findViewById(R.id.host);
portText = (EditText) findViewById(R.id.port);
String hostName = hostText.getText().toString().trim();
String portString = hostText.getText().toString().trim();
int port;
if(portString==null){
port=3000;
}else{
port=Integer.parseInt(portString);
}
if (hostName == null) {
hostName = "192.168.13.245";
}
Log.d(tag, "Input host name is: "+hostName);
SocketClient client = new SocketClient();
client.host = hostName;
client.port = port;
client.start();


正确代码

hostText = (EditText) findViewById(R.id.host);
portText = (EditText) findViewById(R.id.port);
String hostName = hostText.getText().toString().trim();
String portString = hostText.getText().toString().trim();
int port;
try {
port = Integer.parseInt(portString);
} catch (NumberFormatException e) {
port = 3000;
}
if (hostName == null) {
hostName = "192.168.13.245";
}
Log.d(tag, "Input host name is: "+hostName);
SocketClient client = new SocketClient();
client.host = hostName;
client.port = port;
client.start();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: