Ошибка ниже фиксируется удалением декларации, но другая появилась, которая ранее не существовала.Исключение в потоке
Получение двух ошибок, любое понимание было бы замечательным, спасибо.
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)
из ChatServerThread:
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();
}
}
Просьба указать в коде, какие именно строки вызывают эти ошибки. – talnicolas
Какие строки в ваших образцах соответствует NPE. Что-то в этой строке должно быть нулевым. Может быть, вы не вызывали start() где-то? – Gray
Обновленный ответ от обратной связи ниже, но появился другой вопрос с потоком. – Astron