title: How to receive the video stream of a Tello drone using Processing and UDP
body: <p>I am trying to receive the video stream of my Tello drone using Processing as stated in its <a href="https://github.com/grofattila/dji-tello/blob/master/Tello%20SDK%20Documentation%20EN_1.3_1122.pdf" rel="nofollow noreferrer">SDK documentation</a>:
  <a href="https://i.stack.imgur.com/VguPx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VguPx.png" alt="enter image description here"></a></p>
  
  <p>To receive the video stream via UDP I have tried to use <a href="https://github.com/shiffman/Processing-UDP-Video-Streaming/blob/master/VideoReceiver/VideoReceiver.pde" rel="nofollow noreferrer">a script by The Coding Train</a>:</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.awt.image.*; 
  import javax.imageio.*;
  import java.net.*;
  import java.io.*;
  
  
  // Port we are receiving.
  int port = 11111; 
  DatagramSocket ds; 
  // A byte array to read into (max size of 65536, could be smaller)
  byte[] buffer = new byte[65536]; 
  
  PImage video;
  
  void setup() {
    size(400,300);
    try {
      ds = new DatagramSocket(port);
    } catch (SocketException e) {
      e.printStackTrace();
    } 
    video = createImage(320,240,RGB);
  }
  
   void draw() {
    // checkForImage() is blocking, stay tuned for threaded example!
    checkForImage();
  
    // Draw the image
    background(0);
    imageMode(CENTER);
    image(video,width/2,height/2);
  }
  
  void checkForImage() {
    DatagramPacket p = new DatagramPacket(buffer, buffer.length); 
    try {
      ds.receive(p);
    } catch (IOException e) {
      e.printStackTrace();
    } 
    byte[] data = p.getData();
  
    println("Received datagram with " + data.length + " bytes." );
  
    // Read incoming data into a ByteArrayInputStream
    ByteArrayInputStream bais = new ByteArrayInputStream( data );
  
    // We need to unpack JPG and put it in the PImage video
    video.loadPixels();
    try {
      // Make a BufferedImage out of the incoming bytes
      BufferedImage img = ImageIO.read(bais);
      // Put the pixels into the video PImage
      img.getRGB(0, 0, video.width, video.height, video.pixels, 0, video.width);
    } catch (Exception e) {
      e.printStackTrace();
    }
    // Update the PImage pixels
    video.updatePixels();
  }
  </code></pre>
  
  <p>The only thing I changed in the script is the variable <code>port</code> as the SDK documentation (see above) specifies, that it should be <code>11111</code>.</p>
  
  <p>When I run the script (after successfully sending <code>command</code> and <code>streamon</code> to the drone), I get the message <code>Received datagram with 65536 bytes.</code> and a Null Pointer Exception. I've found out, that the Null Pointer Exception is shown, because the following line of code returns <code>null</code>:</p>
  
  <pre class="lang-java prettyprint-override"><code>BufferedImage img = ImageIO.read(bais);
  </code></pre>
  
  <p>So in the end, my question is, why this line of code or more specifically <code>ImageIO.read(bais)</code> returns <code>null</code>. However, the script works correctly, if you use it combined with the <a href="https://github.com/shiffman/Processing-UDP-Video-Streaming/blob/master/VideoSender/VideoSender.pde" rel="nofollow noreferrer">Video Sender script of the Coding Train</a>. So, what's the problem here?</p>