Mysterious exception (NullPointerException)

edited March 2018 in Library Questions

Hi,

I am trying to get to work a simple server-client communication using Processing's Network library.

I want that my server refuses new clients past a certain amount of already connected clients.

Here is my serverEvent method :

void serverEvent(Server _S, Client _C) // is run when a client connects
{
  if(idCount > maxClients)
  {
    _S.disconnect(_C);
    println("Client "+idCount+" refused.");
    idCount ++;
  }
  else {
    S.write(_C.ip()+" "+Integer.toString(idCount));
    println("Client "+idCount+" connected.");
    idCount ++;
  }
}

The else part works fine. But when the program executes the if part, it throws this exception :

java.lang.NullPointerException
    at processing.net.Client.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:748)

However the exception is thrown outside the if brackets : I can print something after the if/else statement before the exception is thrown.

Any hint to solve this ?

Answers

  • What do you mean by "certain amount of already connected clients"? What is the number?

  • edited March 2018

    I've set maxClients = 10. I don't intend to set it to a much larger value.

  • Can you please post a more complete example? It's pretty hard to help you without seeing what's going on. Try narrowing your problem down to a MCVE.

  • Sure !

    To reproduce the problem :
    - Create one sketch for the server and one for the client
    - Run the server, then the client
    - The server throws the exception

    /*
        Server
    */
    
    import processing.net.*;
    
    Server S;
    
    int idCount;
    int maxClients;
    
    void setup()
    {
      size(100, 100);
    
      idCount = 11;
      maxClients = 10;
      S = new Server(this, 5204);
    }
    
    void draw(){}
    
    void serverEvent(Server _S, Client _C) // is run when a client connects
    {
      if(idCount > maxClients)
      {
        _S.disconnect(_C);
        println("Client "+idCount+" refused.");
        idCount ++;
      }
      else {
        println("Client "+idCount+" connected.");
        idCount ++;
      }
      println("End of serverEvent method.");
    }
    

    .

    /*
        Client
    */
    
    import processing.net.*;
    
    Client C;
    
    void setup()
    {
      size(100, 100);
    
      C = new Client(this, "127.0.0.1", 5204);
    }
    
    void draw(){}
    
  • println(_S, _C);
    

    after line 24

  • I ran it a first time. The server didn't throw the exception when I ran the client sketch. I closed the client and ran it again, and then the server threw the exception :

    processing.net.Server@6be9bac5 processing.net.Client@144e887f
    Client 11 refused.
    End of serverEvent method.
    Client SocketException: Socket closed
    processing.net.Server@6be9bac5 processing.net.Client@5674b403
    Client 12 refused.
    End of serverEvent method.
    java.lang.NullPointerException
        at processing.net.Client.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)
    

    I closed the server and started it again and this time it threw the exception on the first client connection.

    processing.net.Server@3e0642fd processing.net.Client@6243d79
    Client 11 refused.
    End of serverEvent method.
    java.lang.NullPointerException
        at processing.net.Client.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)
    
Sign In or Register to comment.