当我的服务器连接到客户端时出现问题,发生此异常,然后我的程序在连接后没有响应。我使用netbeans gui designer为gui类生成代码。我也在我的代码中评论下面例外中显示的行号,供大家考虑。在插座接受之前,一切都能找到。请帮忙!
线程中的异常"线程0" java.lang.NullPointerException at serverui.ServerUi.StartServer(ServerUi.java:62)at serverui.ServerThread.run(ServerUi.java:19)at java.lang.Thread.run(Thread.java:745)
class ServerThread implements Runnable{
public void run(){
try {
ServerUi t = new ServerUi();
t.StartServer(); //Line 19
} catch (IOException ex) {
//Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
//Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class ServerUi {
byte s = 0;
static ServerSocket serverSocket = null;
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
public static boolean checklisten = false;
static gui winframe = null;
public static void main(String[] args)throws IOException {
winframe = new gui();
serverSocket = new ServerSocket(10007);
Thread tserver = new Thread(new ServerThread());
tserver.start();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
winframe.setVisible(true);
}
});
}
public void StartServer()throws IOException, InterruptedException
{
winframe.displayMessage("Listening for Connection");
clientSocket = serverSocket.accept();
winframe.displayMessage("Connected");
String inputLine;
while(true){
while((inputLine=in.readLine()) != null) // Line 62
{
winframe.displayMessage("Server: "+ inputLine);
out.println(inputLine);
if(inputLine.equals("Bye."))
{
this.Closeconnection();
break;
}
if(winframe.checkdisconnectbtn == true)
{
this.Closeconnection();
break;
}
}
}
}
答案 0 :(得分:4)
您永远不会初始化BufferedReader
。
BufferedReader in = null;
...因此这一行失败了NullPointerException
:
while((inputLine=in.readLine()) != null) // Line 62
通常,每当看到NullPointerException
时,您只需查看该行并检查调用null
方法的所有对象。
答案 1 :(得分:2)
你没有初始化。
in = new BufferedReader(arguments);