2014-01-07 46 views
2

试图通过套接字发送arrayList,在对象输入流初始化(客户端)处获取空指针异常。当通过套接字接收数组列表时发生NullPointerException

客户:

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 
} 

我一直在努力,现在几乎没有成功comunicate通过套接字两天。我不知道发生了什么事。 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(); 
     } 
    } 
} 
+1

没有足够的代码在这个问题上给你答案。请告诉我们'Client.socket'的定义和实例。最好张贴证明问题的[SSCCE](http://sscce.org)。 –

+2

您可以提供堆栈跟踪并显示其投诉的行号吗?这通常在这些情况下非常有用。 – CodeChimp

+0

@Duncan Ok会尝试。 @CodeChimp,堆栈跟踪在我的代码中指向'// here' – Kalec

回答

2

因为NPE在这一行:

Client.socket.getInputStream()); 

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

在此行中:

private static Socket socket = null; 

设置socketnull。我看到你设置它不是null的唯一地方是你的.establishConnection()方法,但是我没有看到你调用该方法的地方。

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

2

是你establishConnection方法之前

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

如果没有,你Client.socket为空叫,你需要初始化它。即你的代码应该是这样的:

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

确实。当我开始移动时,我忘了调用'.establishConnection' – Kalec

相关问题