以下错误已通过删除声明得到修复,但出现了另一个以前不存在的错误。
收到两个错误,任何见解都很好,谢谢。
Exception in thread "main" java.lang.NullPointerException at ChatClient.(ChatClient.java:27) at ChatClient.main(ChatClient.java:59)
来自以下 ChatClient:
import java.net.*;
import java.io.*;
public class ChatClient
{ private Socket socket = null;
private BufferedReader console = null;
private BufferedReader streamIn = null;
private DataOutputStream streamOut = null;
public ChatClient(String serverName, int serverPort, String userName)
{ System.out.println("Establishing connection. Please wait...");
try
{ socket = new Socket(serverName, serverPort);
System.out.println("Connected: " + socket);
System.out.println("CTRL+C or type .bye to quit");
start();
}
catch(UnknownHostException uhe)
{ System.out.println("Host unknown: " + uhe.getMessage());
}
catch(IOException ioe)
{ System.out.println("Unexpected exception: " + ioe.getMessage());
}
String line = "";
while (!line.equals(".bye"))
{ try
{ line = console.readLine();
streamOut.writeBytes(line + '\n'); //Send console data to server socket
String reply = streamIn.readLine(); //Recieve confirmation msg from server
System.out.println( reply ); //Print the msg
streamOut.flush();
}
catch(IOException ioe)
{ System.out.println("Sending error: " + ioe.getMessage());
}
}
}
public void start() throws IOException
{ console = new BufferedReader(new InputStreamReader(System.in)); //Changed console to BufferedReader
streamIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
streamOut = new DataOutputStream(socket.getOutputStream());
}
public void stop()
{ try
{ if (console != null) console.close();
if (streamOut != null) streamOut.close();
if (streamIn != null) streamIn.close(); //Is it good practice to close
if (socket != null) socket.close();
}
catch(IOException ioe)
{ System.out.println("Error closing ...");
}
}
public static void main(String args[])
{ ChatClient client = null;
if (args.length != 3)
System.out.println("Usage: java ChatClient host port username");
else
client = new ChatClient(args[0], Integer.parseInt(args[1]), args[2]);
}
}
这个错误:
Exception in thread "Thread-1" java.lang.NullPointerException
at ChatServerThread.handleClient(ChatServerThread.java:41)
at ChatServerThread.run(ChatServerThread.java:17)
来自聊天服务器线程:
import java.net.*;
import java.io.*;
//public class ChatServerThread implements Runnable
public class ChatServerThread extends Thread
{ private Socket socket = null;
private ChatServer server = null;
private int ID = -1;
private BufferedReader streamIn = null;
private DataOutputStream streamOut = null;
public ChatServerThread(ChatServer _server, Socket _socket)
{ server = _server; socket = _socket; ID = socket.getPort();
}
public void run() {
try {
handleClient();
} catch( EOFException eof ) {
System.out.println("Client closed the connection.");
} catch( IOException ioe ) {
ioe.printStackTrace();
}
}
public void handleClient() throws IOException {
boolean done = false;
try {
System.out.println("Server Thread " + ID + " running.");
while (!done) {
String nextCommand = streamIn.readLine();
if( nextCommand.equals(".bye") ) {
System.out.println("Client disconnected with bye.");
done = true;
} else {
System.out.println( nextCommand );
String nextReply = "You sent me: " + nextCommand.toUpperCase() + '\n';
streamOut.writeBytes ( nextReply );
}
}
} finally {
streamIn.close();
streamOut.close();
socket.close();
}
}
public void open() throws IOException
{
streamIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
streamOut = new DataOutputStream(socket.getOutputStream());
}
public void close() throws IOException
{ if (socket != null) socket.close();
if (streamIn != null) streamIn.close();
if (streamOut != null) streamOut.close();
}
}
最佳答案
是的,就是这一行:
line = console.readLine();
console
仍然是 null。即使您正在调用 start()
,它也不会像您想的那样工作:
public void start() throws IOException
{ BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
这声明了一个名为 console
的新本地 变量。它不会更改名为 console
的 instance 变量的值。为此,您应该删除声明部分:
public void start() throws IOException
{
console = new BufferedReader(new InputStreamReader(System.in));
...
即使进行了这种更改,您也可能会遇到问题 - 因为如果那确实抛出异常,那么您在构造函数中使用它进行的操作如下:
catch(IOException ioe)
{ System.out.println("Unexpected exception: " + ioe.getMessage());
}
然后你就好像什么都没发生一样继续。不要那样做。您并没有真正“处理”异常 - 所以您几乎可以肯定要么不首先捕获它,要么在您的 catch block 中重新抛出它。
顺便说一句,您的音梁风格非常密集、非常规且不一致。我强烈建议不要在左大括号之后(在同一行)包含代码。事实上,您的代码对于习惯于(非常)更常见的约定的任何人来说都很难阅读:
if (foo) {
// Do something
}
或
if (foo)
{
// Do something
}
关于java - 线程异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9504806/