title: indexoutofboundsexception by sending pictures over Socket
body: <p>I want to make a little programm that makes a live-stream for Desktop.
  It should be so that you send pictures to an echo-server and he response it to the clients.
  There you get be draw the Images. Side by Side. And so it is like a movie(or something like that).
  But I always get an indexoutofboundsexception. Where is the error or how can I improve my program.</p>
  
  <p>The ImageIO.write lines thows the Error</p>
  
  <p>//Client Main</p>
  
  <p>public class Main {</p>
  
  <pre><code>public static void main(String[] args) {
  
      Frame frm = new Frame();
  
      Frame.Client client;
  
      frm.setLayout(null);
      frm.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
      frm.setResizable(false);
      frm.setSize(1600,900);
      frm.setVisible(true);
  
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>// get and send the Desktopimage</p>
  
  <p>public class desktopCapture {</p>
  
  <pre><code>Robot robo;
  BufferedImage screenImage;
  Rectangle bounding;
  
  public desktopCapture() {
      try {
          bounding = new Rectangle(0,0,1600,900);
          robo = new Robot();
      } catch (AWTException e) {e.printStackTrace();}
  }
  
  public void sendScreenCapture(Socket client) {
      screenImage = robo.createScreenCapture(bounding);
      try {
          ImageIO.write(screenImage, "png", client.getOutputStream());
  
      } catch (IOException e) {e.printStackTrace();}
  }
  </code></pre>
  
  <p>}</p>
  
  <p>// in Frame two function for actionListener Objects, so I can say who streams his Desktop and which get only the Images to.
  public void readImage() {
              while(true) {</p>
  
  <pre><code>            try {
                  while((screenImage = ImageIO.read(in)) != null){
  
                      repaintScreen();
  
                  }
              } catch (IOException e) {e.printStackTrace();}
          }
      }
  </code></pre>
  
  <p>public void sendImage() {</p>
  
  <pre><code>        try {
              while(true){
              dC.sendScreenCapture(client);
              System.out.println("read1");
              while((screenImage = ImageIO.read(in)) != null){
              System.out.println("read2");
              ImageIO.write(screenImage, "png", new File("image1.png"));
  
              Thread.sleep(250);
              }
              repaintScreen();
              screenImage = null;
  
              }   
              } catch (IOException | InterruptedException e) {e.printStackTrace();}
  
       }
      }
  
  }
  </code></pre>
  
  <p>//Thread for a Client</p>
  
  <p>public class handler implements Runnable {</p>
  
  <pre><code>Socket client;
  OutputStream out;
  InputStream in; 
  PrintWriter writer;
  BufferedImage image;
  
  public handler(Socket client) {
      this.client = client;
  }
  
  @Override
  public void run() {
  
      try {
  
          in = client.getInputStream();       
      BufferedReader reader = new BufferedReader(new InputStreamReader(in));
      while(true) {
          System.out.println("write1");
          while((image = ImageIO.read(in)) != null){
              System.out.println("write2");
      for(int i = 1;i &lt;= Server.connectionArray.size();i++){
          Socket TEMP_SOCK = (Socket)Server.connectionArray.get(i-1);
       out = TEMP_SOCK.getOutputStream();
       writer = new PrintWriter(out);
  
      ImageIO.write(image, "png", TEMP_SOCK.getOutputStream());
      System.out.println("write3");
          }
        image = null;
       }
      }
  
      } catch (IOException e) {e.printStackTrace();}
  }
  </code></pre>
  
  <p>}</p>