【问题标题】:NullPointerException when receiving an array list over a socket通过套接字接收数组列表时出现 NullPointerException
【发布时间】:2014-01-07 12:53:47
【问题描述】:

试图通过套接字发送一个arrayList,在对象输入流初始化时得到一个空指针异常(客户端)。

客户:

try {
        ObjectInputStream objIn = new ObjectInputStream(
               Client.socket.getInputStream()); // HERE
        library = (ArrayList<Book>) objIn.readObject();
    } catch (IOException e) {

服务器:

try {
    ObjectOutputStream objOut = new ObjectOutputStream(
            this.client.getOutputStream());
    objOut.writeObject(library);
    objOut.flush(); // added later, not helping
}

我已经尝试通过套接字进行通信两天了,但几乎没有成功。我不知道是怎么回事。 Ofc 我计划在我有更多时间的时候更好地记录自己,但现在我真的很想了解正在发生的事情。

编辑

public class Client {

    private static int  port    = 6666;
    private static Socket   socket  = null;

    public Client (int port) {
        Client.port = port;
    }

    public Client () {
    }

    public void establishConnection() {
        try {
            Client.socket = new Socket(InetAddress.getByName(null), Client.port);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

服务器:

public void start () {
    (new Thread() {
        public void run() {
            try {
                Server.socket = new ServerSocket(Server.portNumber);
                while (!Server.stop) {
                    Socket client = Server.socket.accept();
                    (new HandleRequest (client)).start();
                }
             ...............


public class HandleRequest extends Thread {

    private Socket client = null;
    private SQL sql_db = new SQL ();

    public HandleRequest (Socket client) {
        this.client = client;
    }

    @Override
    public void run () {
        try {
            if (!this.sql_db.isConnected())
                this.sql_db.connect();
            if (this.client == null) {
                System.out.println("Error: client does not exist, NO idea what's going on");
                return;
            }



            ArrayList<Book> library = this.sql_db.getAllBooks();
            try {
                ObjectOutputStream objOut = new ObjectOutputStream(
                        this.client.getOutputStream());
                objOut.writeObject(library);
                objOut.flush();
            } catch (Exception e) {
                System.out.println("Server error in handling request for whole library!");
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

  • 此问题中的代码不足,无法为您提供答案。请告诉我们Client.socket 的定义和实例化位置。最好发布一个 SSCCE 来说明问题。
  • 你能提供堆栈跟踪并显示它抱怨的行号吗?这在这些情况下通常很有帮助。
  • @Duncan Ok 会尝试的。 @CodeChimp,堆栈跟踪指向我的代码中的//here
  • 你有没有调用过ClientestablishConnection()方法?

标签: java sockets ipc


【解决方案1】:

因为 NPE 在这一行:

Client.socket.getInputStream());

只有一件事可以导致它。不能是Client,因为那是static。不可能是getInputStream(),因为那是一种方法,所以导致NPE的一定是socket

在这一行:

private static Socket socket = null;

您将socket 设置为null。我看到您将其设置为非 null 的唯一位置是在您的 .establishConnection() 方法中,但我看不到您在哪里调用该方法。

因此,您的问题很可能是您没有调用.establishConnection() 方法。

【讨论】:

    【解决方案2】:

    你之前调用过establishConnection方法吗

    try {
        ObjectInputStream objIn = new ObjectInputStream(
               Client.socket.getInputStream()); // HERE
        library = (ArrayList<Book>) objIn.readObject();
    } catch (IOException e) {
    

    如果不是,您的 Client.socket 为空,您需要对其进行初始化。 IE。您的代码应如下所示:

    try {
        Client c = new Client(1337);
        c.establishConnection();
        ObjectInputStream objIn = new ObjectInputStream(
               c.socket.getInputStream()); // HERE
        library = (ArrayList<Book>) objIn.readObject();
    } catch (IOException e) {
    

    【讨论】:

    • 确实如此。当我开始搬东西时,我忘了打电话给.establishConnection
    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 2021-09-30
    • 2013-05-19
    • 1970-01-01
    • 2018-05-29
    相关资源
    最近更新 更多