title: Why are my getters returning null when I try to send to the server?
body: <p>I know this question has been asked a countless number of times on here, and I've searched SO and other sources for a solution but I just can't resolve the error. I have a getter and setter for my ClientPlayer class attribute, the setter is called in the GUI when a certain button is called, and I would like to use the getter after a client connects and send the object to the server. Calling the method "this.client.sendTCP(clientPlayer.getPlayerName());" in the ClientController returns a nullPointerException. </p>
  
  <p>Error: "JavaFX Application Thread" java.lang.IllegalArgumentException: object cannot be null.</p>
  
  <p>My guess would be that I create a new instance of ClientPlayer one time too many thus returning null as my string playerName is originally set to none. However, I am not sure on how to go about solving the problem. I would really appreciate any help whatsoever.</p>
  
  <pre><code>public class ClientPlayer implements Serializable {
  
  public ClientPlayer() {
  
  }
  
  private String playerName;
  
  public void setPlayerName(String playerName) {
      this.playerName = playerName;
     }
  
  public String getPlayerName() {
      return this.playerName;
     }
  }  
  </code></pre>
  
  <p>Below the relevant part of my GUI-code where I set a button to connect the to the server: </p>
  
  <pre><code>    ClientPlayer clientPlayer = new ClientPlayer();  
  
      clientToGame.setOnAction((ActionEvent w) -&gt; {
           clientPlayer.setPlayerName(clientNameText.getText());
           clientController.connect();
           window.setScene(lobbyScene);
      });
  </code></pre>
  
  <p>Here is the bit of my client-class where I try to get the name and send to server:</p>
  
  <pre><code>    public class ClientController() {
       ClientPlayer clientPlayer = new ClientPlayer();
  
      public void connect() {
      if (client.isConnected()) {
          Logger.getLogger(getClass().getName()).log(Level.INFO, "You are      already connected to :{0}", config.getHost());
          return;
      }
      this.client.start();
      try {
          this.client.connect(5000, config.getHost(), config.getTCPPort());
          System.out.println("Successfully connected to " + config.getHost());
          this.client.sendTCP(clientPlayer.getPlayerName());
  
      } catch (IOException ex) {
          Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Server connection failed: {0}", ex.getMessage());
          throw new RuntimeException(ex);
      }
  
      MessageRegistry.registerMessages(client.getKryo());
      this.client.addListener(listener);
    }
   }
  </code></pre>