当我尝试用Java将对象写入服务器时获取NullPointerException

时间:2014-09-29 11:23:42

标签: java nullpointerexception serversocket

我有一个小问题。我试图在另一个类中使用一个方法将对象发送到我拥有的服务器。我正在使用带套接字的Java。

方法:

public void sendMessageToServer(String message) {

    if (message != null) {
        try {
            serverComManager.outputStream.writeObject(message);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

尝试使用该方法的类:

        sendMessage.sendMessageToServer("Hello");

该行引发错误:

    serverComManager.outputStream.writeObject(message);

提前谢谢! 〜录音棚

编辑:根据要求,我添加了'serverComManager'声明以及该类的代码。我还包括了完整的错误。我希望这可以帮助你理解我的问题。

声明:

    ServerCommunicationManager serverComManager = new ServerCommunicationManager();

ServerCommunicationManager的代码:

boolean connected;


//Setup
Socket clientSocket;
ObjectOutputStream outputStream;
ObjectInputStream inputStream;

public boolean connectToHost() throws UnknownHostException, IOException{


    clientSocket = new Socket("localhost", 2444);
    setupStreams(clientSocket);

    if(clientSocket.isConnected()){
        connected = true;
    }else{
        connected = false;
    }


    return connected;

}

private void setupStreams(Socket s) throws IOException{

    outputStream = new ObjectOutputStream(s.getOutputStream());
    inputStream = new ObjectInputStream(s.getInputStream());

}

错误:

Exception java.lang.NullPointerException
at SendToServer.sendMessageToServer(SendToServer.java:16)
at DissconnectClient.dissconnectFromServer(DissconnectClient.java:15)

错误行:

DissconnectClient 15: sendMessage.sendMessageToServer(abortConnectionKeyword);
SendToServer 16: serverComManager.outputStream.writeObject(message);

注意:DisconnectClient是我正在编写的类之一。这是类代码:

    public class DissconnectClient {

    //Variables
    private final String keyword = "DISSCONNECT";


    //Setup
    SendToServer sendMessage = new SendToServer();


    public void dissconnectFromServer(){

        sendMessage.sendMessageToServer(keyword);

    }

}

1 个答案:

答案 0 :(得分:1)

我看不到你在哪里分配" serverComManager"或者你在哪里创造一个这样的意义。也许在构造函数方法中,使用方法" sendMessageToServer"你正在做类似this.serverComManager =(...)的事情。我不确定你是如何处理" serverComManager"的逻辑的?但到目前为止,我解决问题的方法如下(如果我正在写一个向服务器发送消息的客户端)。考虑到没有为您的" serverConnManager"提供代码,也许您可​​以识别当前实现中缺少的内容。

public void sendMessageToServer(String message) {

    if (message != null) {
        try {
            //Assume I already have an instance of client Socket:
            //Socket outgoingConn = new Socket(host, port)
            //1. I get the OutputStream of my outgoing connection
            OutputStream outStream = outgoingConn.getOutputStream();
            //2. Create an object output stream
            ObjectOutputStream objectWriter = new ObjectOutputStream(outStream);
            //3. Write the object
            objectWriter.writeObject(message);
            //Close the io if required (would recommend try-with-resources if using jdk7)
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

如果您只使用String消息,BufferedWriter或BufferedReadr就足够了,如果您尝试处理可以同时使用" beans"或者字符串,如果你创建类似的东西会更好:

public class MessageBean implements Serializable {
    private MessageType type;
    private Object param;
    //Getters/Setters
}

MessageType可以是指定所支持对象类型的枚举,因为param字段是可以作为String或bean处理的对象。然后根据MessageType或使用" instanceof"进行工作。但是,如果你想进一步尝试,这只是一个建议。

希望它有所帮助。快乐的编码!

问候。