为什么我的应用程序在执行此代码时崩溃? [英] Why my app crashing executing this code?
本文介绍了为什么我的应用程序在执行此代码时崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Android. Java.Lang.NullPointerException执行以下代码:
尝试 {
// sc.setReceiveBufferSize(163840);
sc = 新套接字(" span>, 8090 );
SocketAddress sa = 新 InetSocketAddress(" , 8090 );
sc.connect(sa);
} 捕获(异常x)
{
logn.setText(x.toString()); // Java.Lang.NullPointerException,为什么?
}
登录在OnCreate函数中声明,并且我写了"EditText登录;"在班上
我已经注释了"sc.setReceiveBufferSize(163840);",因此现在应用程序崩溃了,没有任何异常.观点.查看您的代码,找出原因.如果您对运行时行为的关注最小,请始终使用调试器.
第二个谢尔盖的回答是,套接字连接失败.
您还应该这样写:
尝试 { sc.setReceiveBufferSize( 163840 ); sc = 新套接字(" span>, 8090 ); SocketAddress sa = 新 InetSocketAddress(" , 8090 ); sc.connect(sa); } 捕获(异常x){ System.out.println(x.printStackTrace()); // 以获取完整的异常 }
我还建议对变量进行更好的命名-您将不熟悉sc/sa/s ....将它们命名为"socket"和"address",依此类推.
Android. Java.Lang.NullPointerException execiting this code:
try{
//sc.setReceiveBufferSize(163840);
sc = new Socket("46.247.186.175", 8090);
SocketAddress sa = new InetSocketAddress("46.247.186.175", 8090);
sc.connect(sa);
}catch(Exception x)
{
logn.setText(x.toString());//Java.Lang.NullPointerException, Why?
}
EDIT: logn is declared in OnCreate func and I wrote "EditText logn;" in the Class
EDIT2: I have commented "sc.setReceiveBufferSize(163840);", so now app crashing without any exceptions.
解决方案
Apparently, becauselogn
is null at this point. Look at your code and find out why. Should you have a smallest concern about your runtime behavior, always use the debugger.
—SA
I second Sergey''s answer, the Socket Connection is failing.
You should also write that different:
try{ sc.setReceiveBufferSize(163840); sc = new Socket("46.247.186.175", 8090); SocketAddress sa = new InetSocketAddress("46.247.186.175", 8090); sc.connect(sa); } catch(Exception x) { System.out.println(x.printStackTrace()); // to get the full exception }
I also recommend better naming of variables - you will loose track with sc/sa/s... . Name them "socket" and "address" and so on.
这篇关于为什么我的应用程序在执行此代码时崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文