Java 为什么Android中的ServerSocket会出现NullPointerException
我的新项目有一些重大问题。我的电脑上有一个客户端应用程序,通过WiFi LAN连接向手机发送简单的数据字符串。收到时,应将其拆分为其组成部分,然后发送SMS 老实说,这是我的第一个Android项目,但我决心让它工作起来 当我从客户端发送命令时,应用程序失败,并被标记为Java 为什么Android中的ServerSocket会出现NullPointerException,java,android,sockets,serversocket,Java,Android,Sockets,Serversocket,我的新项目有一些重大问题。我的电脑上有一个客户端应用程序,通过WiFi LAN连接向手机发送简单的数据字符串。收到时,应将其拆分为其组成部分,然后发送SMS 老实说,这是我的第一个Android项目,但我决心让它工作起来 当我从客户端发送命令时,应用程序失败,并被标记为Log.w(“ServerRunnable”,“Exception 2”)的捕获捕获捕获如下。我真的很感激你给我一些建议?我不知道为什么它不起作用 public class ListeningService extends Ser
Log.w(“ServerRunnable”,“Exception 2”)的捕获捕获捕获代码>如下。我真的很感激你给我一些建议?我不知道为什么它不起作用
public class ListeningService extends Service {
private int iNotificationIcon = 1;
Handler hHandler;
Thread thServerThread = null;
private ServerSocket oServerSocket = null;
private static final String _S_DELIMITER = "##t2tDELIMITER##";
private static final int _I_SERVER_PORT = 50001;
@Override
public IBinder onBind(Intent arg0) {
Toast.makeText(this, "Service Bound", Toast.LENGTH_LONG).show();
return null;
}
@Override
public int onStartCommand(Intent oIntent, int iFlags, int iStartID) {
addNotification(getString(R.string.app_name), "Server listening on xxx.xxx.xxx.xxx:ppppp", iNotificationIcon, true);
/* Server Threading */
/*hHandler = new Handler() {
@Override
public void handleMessage(Message mMsg) {
// TODO Auto-generated method stub
showMessageBox(mMsg.getData().toString(), "Data Bundle", true);
super.handleMessage(mMsg);
}
};*/
this.thServerThread = new Thread(new ServerRunnable());
//showToast("Thread Created but not started yet", false);
this.thServerThread.start();
return START_STICKY;
}
public class ServerRunnable implements Runnable {
public ServerRunnable() {
/**/
}
public void run() {
Socket oSocket;
try {
oServerSocket = new ServerSocket(_I_SERVER_PORT);
Log.w("ServerRunnable", "oServerSocket created");
} catch (Exception e) {
e.printStackTrace();
Log.w("ServerRunnable", "Exception 1");
}
while (!Thread.currentThread().isInterrupted()) {
try {
oSocket = oServerSocket.accept();
CommsThread commThread = new CommsThread(oSocket);
new Thread(commThread).start();
Log.w("ServerRunnable", "oSocket created");
} catch (Exception e) {
e.printStackTrace();
Log.w("ServerRunnable", "Exception 2");
}
}
}
}
public class CommsThread implements Runnable {
private Socket oClientSocket;
private BufferedReader brInput;
public CommsThread(Socket oSocket) {
this.oClientSocket = oSocket;
try {
this.brInput = new BufferedReader(new InputStreamReader(this.oClientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
String sRead = brInput.readLine();
//hHandler.post(new SendSMSThread(sRead));
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public class SendSMSThread implements Runnable {
String sPhoneNumber = "";
String sTextMessage = "";
int iMessageLength = 0;
public SendSMSThread(String sRead) {
/* Received */
//showMessageBox(sRead, "Raw Data", true);
int iDelimiterStart = sRead.indexOf(_S_DELIMITER);
int iDelimiterLen = _S_DELIMITER.length();
/* Split the input */
sPhoneNumber = sRead.substring(0, (iDelimiterStart+1));
sTextMessage = sRead.substring((iDelimiterStart + iDelimiterLen));
//showMessageBox(sTextMessage, sPhoneNumber, true);
}
public void run() {
if ((sPhoneNumber.length() == 11) && (sTextMessage.length() > 0)) {
sendSMS(sPhoneNumber, sTextMessage, true);
}
}
}
堆栈跟踪:
01-23 23:48:42.673: W/ServerRunnable(15567): Exception 2
01-23 23:48:42.673: W/StackTrace(15567): java.lang.NullPointerException
01-23 23:48:42.673: W/StackTrace(15567): at uk.co.tip2tail.wintextserver.ListeningService$ServerRunnable.run(ListeningService.java:78)
01-23 23:48:42.673: W/StackTrace(15567): at java.lang.Thread.run(Thread.java)
这似乎只有在我停止并重新启动服务后才会发生。服务的初始运行似乎还可以,尽管在接收数据时它仍然不做任何事情!我完全迷路了。我读过的每本书都有和我一样的代码,但这根本不起作用
非常感谢您的帮助。我的事情太复杂了
此后,我返回并完全更改了服务的代码,只运行了2个线程
现在它可以工作了
感谢您提出上述建设性意见。:) 你能包括堆栈跟踪吗?嗨@AndrewFielden上面更新了:)呃,哪一行是第78行?显然,您正在该行取消对空引用的引用。可能是因为您的奇怪做法,即捕获异常,然后让代码继续运行,就好像它们没有发生一样。以前有例外吗?例如,创建ServerSocket?OMG,您正在应用程序中启动4个新线程(如果我没有错的话)。你是在使用深蓝色的安卓设备还是什么?:)您可以很容易地摆脱CommsThread
,例如,在同一线程中读取clientSocket中的行是非常好的。我也不确定你是否真的需要在你的应用程序中重新发明轮子。也许值得一看针对Android的GCM-starndard异步消息传递?无论如何,回到最初的问题。在这里,最好的做法是在第78行设置一个断点,并在调试模式下运行应用程序。这将帮助您确定发生空指针异常的原因。