我正嘗試創建一個多線程服務器和客戶端對。這兩個類都是繼承的方法,用於寫入和從一個普通類讀取..我無法正確工作,並且正在接收空指針異常。 這裏是我的代碼:Java多線程服務器/客戶端邏輯不工作
//服務器和客戶端的常規類
package server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import exception.AutoException;
public class DefaultSocketClient extends Thread {
protected ObjectInputStream ois;
private ObjectOutputStream oos;
private Socket sok;
public DefaultSocketClient(Socket sok) {
this.sok = sok;
}
public void run() {
openConnection();
handleSession();
// closeSession();
}
/*
* this methods opens connection
*/
public void openConnection() {
try {
ois = new ObjectInputStream(sok.getInputStream());
oos = new ObjectOutputStream(sok.getOutputStream());
} catch (IOException e) {
try {
throw new AutoException("OpenConnectionException");
} catch (AutoException e1) {
e1.printStackTrace();
}
}
}
public void handleSession() {
Object input;
try {
while ((input = ois.readObject()) != null) {
handleInput(input);
}
} catch (Exception e) {
try {
throw new AutoException("HandleSessionException");
} catch (AutoException e1) {
e1.printStackTrace();
}
}
}
private void handleInput(Object newInput) {
System.out.println(newInput);
}
public void sendOutput(Object newOutput) {
try {
oos.writeObject(newOutput);
} catch (IOException ioe) {
try {
throw new AutoException("ObjectOutputException");
} catch (AutoException e1) {
e1.printStackTrace();
}
}
}
/**
* This method closes the Session between client and server
*/
public void closeSession() {
try {
ois.close();
ois.close();
sok.close();
} catch (IOException e) {
try {
throw new AutoException("SessionCloseException");
} catch (AutoException e1) {
e1.printStackTrace();
}
}
}
}
//客戶
/*
* creating client
*/
public AutoClientSocket() {
try {
clientSocket = new Socket(InetAddress.getLocalHost(),
DEFAULT_PORT_NO);
readFromConsole = new BufferedReader(new InputStreamReader(
System.in));
} catch (Exception e) {
try {
throw new AutoException("AutoServerConnectionException");
} catch (AutoException e1) {
e1.printStackTrace();
}
}
}
// starting the client
public void startAutoClient() {
try {
defaultSocketClient = new DefaultSocketClient(clientSocket);
defaultSocketClient.start();
System.out.println("Client started...");
defaultSocketClient.closeSession();
// performOperation();
} catch (Exception e) {
try {
throw new AutoException("ConnectionException");
} catch (AutoException e1) {
e1.printStackTrace();
}
}
}
public void performOperation() {
// methods for client operations.
}
}
//服務器
public class AutoServerSocket {
private int DEFAULT_PORT_NO = 7900;
private static ServerSocket autoServer;
ObjectOutputStream oos;
ObjectInputStream ois;
private DefaultSocketClient defaultSocketClient;
BuildAuto build = new BuildAuto();
FileIO io = new FileIO();
// creating server
public AutoServerSocket() {
try {
autoServer = new ServerSocket(DEFAULT_PORT_NO);
System.out.println("Server started...");
} catch (Exception e) {
try {
throw new AutoException("AutoServerConnectionException");
} catch (AutoException e1) {
e1.printStackTrace();
}
}
}
// starting the server
public void startAutoServer() {
Socket sok;
while (true) {
try {
sok = autoServer.accept();
defaultSocketClient = new DefaultSocketClient(sok);
defaultSocketClient.start();
System.out.println("Connection Established....");
defaultSocketClient.sendOutput(generateAutoWelcome());
defaultSocketClient.handleSession();
defaultSocketClient.closeSession();
} catch (IOException e) {
try {
throw new AutoException("ConnectionException");
} catch (AutoException e1) {
e1.printStackTrace();
}
}
}
}
/**
* This method generates Welcome message for AutoWorld
*/
private String generateAutoWelcome() {
return "--------Welcome to AutoWorld-----------";
}
}
我在服務器收到以下異常 - >
Exception in thread "main" java.lang.NullPointerException
at server.DefaultSocketClient.sendOutput(DefaultSocketClient.java:64)
at server.AutoServerSocket.startAutoServer(AutoServerSocket.java:51)
at driver.ServerDriver.main(ServerDriver.java:11)
在線路:
oos.writeObject(newOutput);
我清楚在這裏做得不對,因爲我不能夠接受的對象我在客戶端發送。有人可以幫幫我嗎?
感謝
異常跟蹤?在代碼中標出發生異常的行 –
我在主帖中對其進行了編輯。感謝 – k19
您在'startAutoClient'和'startAutoServer'中創建了一個新的'DefaultSocketClient',但是您不會調用'start'。相反,您從當前實例調用start,從不調用您在方法中創建的實例。我相信你的問題在那裏 –