gpt4 book ai didi

ServerSocker.accept() 处的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 11:33:37 25 4
gpt4 key购买 nike

我遇到以下异常:

java.lang.NullPointerException at
gr.test.asterisk.AsteriskServerSocket.run(AsteriskServerSocket.java:72)

这是我的代码:

public class AsteriskServerSocket  extends Thread{

private Socket server = null;
private ServerSocket serverSocket = null;
private String serverPortString = null;

public AsteriskServerSocket(){
serverPortString = "50050";

try {
serverSocket = new ServerSocket(Integer.parseInt(serverPortString));
serverSocket.setSoTimeout(1000000000);
} catch (SocketTimeoutException s) {
WriteExceptions.writeToFile(s);
} catch (IOException e) {
WriteExceptions.writeToFile(e);
}
}

public void run() {
while (true) {
try {
server = serverSocket.accept();<---- This is the line which I get the exception
......
}
} catch (SocketTimeoutException s) {
System.err.println("Σφάλμα στην Server run. Socket timed out. \n" + s);
break;
} catch (IOException e) {
System.err.println("Σφάλμα στην Server run.\n" + e);
break;
}
}
}
}

还有AsteriskServerSocket,我在主类中调用它:

public static void main(String[] args) {
AsteriskServerSocket aSS = new AsteriskServerSocket();
}

我的操作系统是 CentOS 版本 5.10(最终版),我的 JDK 版本是 1.8.0_45。我不知道为什么会抛出这个异常。我在这里遗漏了什么吗?

我在构造函数中的异常是:

My exception in the constructor is 0 :java.net.BindException: Address already in use at java.net.PlainSocketImpl.socketBind(Native Method) at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:382) at java.net.ServerSocket.bind(ServerSocket.java:375) at java.net.ServerSocket.(ServerSocket.java:237) at java.net.ServerSocket.(ServerSocket.java:128)

最佳答案

您忽略构造函数中的异常并将其写入日志,因此 serverSocket 为 null。检查日志和/或重新抛出这些异常。

关于ServerSocker.accept() 处的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30221493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com