2

Trying to send an arrayList over a socket, get a null pointer exception at object input stream initialization (client).

Client:

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

Server:

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

I've been trying to comunicate over sockets for two days now with almost no success. I have no idea what's going on. Ofc I plan to document myself better when I'll have more time but for now I'd really like to understand what is happening.

EDIT

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();
        }

Server:

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();
        }
    }
}
Kalec
  • 2,681
  • 9
  • 30
  • 49
  • 1
    Not enough code in this question to give you the answer. Please show us where `Client.socket` is defined and instantiated. Preferably post an [SSCCE](http://sscce.org) that demonstrates the problem. – Duncan Jones Jan 07 '14 at 12:56
  • 2
    Can you provide the stack trace and show the line number that it complained on? That is usually very helpful in these situations. – CodeChimp Jan 07 '14 at 12:56
  • @Duncan Ok will try. @CodeChimp, the stack trace points to `//here` in my code – Kalec Jan 07 '14 at 12:57
  • See: http://www.tutorialspoint.com/java/io/objectinputstream_readobject.htm – Parkash Kumar Jan 07 '14 at 13:00
  • @ParkashKumar Not helping. It's just not so easy, not at all. It is not nearly detailed enough, and this is not the only problem I've had. Aparently I can't even mix BuffereReader and ObjectInputStream: http://stackoverflow.com/questions/6315247/java-io-streamcorruptedexception-invalid-stream-header-75720002 – Kalec Jan 07 '14 at 13:05
  • 1
    Do you ever call `Client`'s `establishConnection()` method? – The Guy with The Hat Jan 07 '14 at 13:06
  • @RyanCarlson Yes I do. Or maybe I messed up when I modified something ? let me check. – Kalec Jan 07 '14 at 13:08
  • @Kalec: Where do you call it? – The Guy with The Hat Jan 07 '14 at 13:08
  • I have a handler, should be called either in the handler's constructor or maybe before and pass the socket to the handler. That was indeed the problem, thank you. – Kalec Jan 07 '14 at 13:13

2 Answers2

2

Is you establishConnection method called before

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

If not, your Client.socket is null and you need to initialize it. I.e. your code should look like this:

try {
    Client c = new Client(1337);
    c.establishConnection();
    ObjectInputStream objIn = new ObjectInputStream(
           c.socket.getInputStream()); // HERE
    library = (ArrayList<Book>) objIn.readObject();
} catch (IOException e) {
mvieghofer
  • 2,846
  • 4
  • 22
  • 51
2

Because the NPE is on this line:

Client.socket.getInputStream());

there is only one thing that can cause it. It can't be Client, because that is static. It can't be getInputStream(), because that is a method, so it has to be socket that is causing the NPE.

On this line:

private static Socket socket = null;

you set socket to be null. The only place I see where you set it to be not null is in your .establishConnection() method, but I don't see where you call that method.

Therefore, your problem is most likely that you aren't calling the .establishConnection() method.

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75