读书人

空指针错误有关问题!java

发布时间: 2012-02-01 16:58:19 作者: rapoo

空指针异常问题!!!java
int port=1025;
port=Integer.parseInt(text_port.getText());


此处代码出现异常:
Exception in thread "main" java.lang.NullPointerException
at ok.Cshell.createSShell(Cshell.java:126)---->port=Integer.parseInt(text_port.getText());

at ok.Cshell.main(Cshell.java:86)---->thisClass.createSShell();注释:port所在的函数:creatSShell


我已经初始化port了。怎么还会出现异常。

[解决办法]
text_port == null
[解决办法]
text_port为null啊
好好检查一下
[解决办法]
text_port.getText() 是 null的.
[解决办法]
text_port.getText()界面没有输入port的值所以造成了空异常
这么写,在发生异常的时候保证port也有默认值:


Java code
int port=1025;try{port=Integer.parseInt(text_port.getText()); //如果这里转换失败,比如输入了数字,或没输入}catch(Exception e){}
[解决办法]
text_port为null啊
[解决办法]
很明显 text_port.getText() 取不到值,当然报空指针错误
[解决办法]
那么就text_port.getText() == null
[解决办法]
text_port.getText() 返回是null

读书人网 >Java相关

热点推荐