Java Gui线程编码?
Java Gui Thread Coding?
当我的服务器连接到客户端时出现问题,出现此异常,然后我的程序在连接后没有响应。我使用 netbeans gui designer 为 gui class 生成代码。我还在我的代码中注释了下面异常显示的行号,供你们考虑。在套接字接受之前找到一切工作。请帮忙!
Exception in thread "Thread-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;
}
}
}
}
您从未初始化 BufferedReader
。
BufferedReader in = null;
... 因此此行失败并显示 NullPointerException
:
while((inputLine=in.readLine()) != null) // Line 62
一般来说,只要看到 NullPointerException
,您所要做的就是查看该行并检查所有正在调用 null
方法的对象。
你没有初始化。
in = new BufferedReader(arguments);
当我的服务器连接到客户端时出现问题,出现此异常,然后我的程序在连接后没有响应。我使用 netbeans gui designer 为 gui class 生成代码。我还在我的代码中注释了下面异常显示的行号,供你们考虑。在套接字接受之前找到一切工作。请帮忙!
Exception in thread "Thread-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;
}
}
}
}
您从未初始化 BufferedReader
。
BufferedReader in = null;
... 因此此行失败并显示 NullPointerException
:
while((inputLine=in.readLine()) != null) // Line 62
一般来说,只要看到 NullPointerException
,您所要做的就是查看该行并检查所有正在调用 null
方法的对象。
你没有初始化。
in = new BufferedReader(arguments);