# Unable to run Motion Sensor example provided by Daniel on Pi, Error when trying to use copy

**URL:** <https://discourse.processing.org/t/unable-to-run-motion-sensor-example-provided-by-daniel-on-pi-error-when-trying-to-use-copy/1635>\
**Category:** Processing for Pi\
**Created:** [July 9, 2018, 5:38pm UTC](https://discourse.processing.org/t/unable-to-run-motion-sensor-example-provided-by-daniel-on-pi-error-when-trying-to-use-copy/1635 "2018-07-09T17:38:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phaneeth](https://avatars.discourse-cdn.com/v4/letter/p/dc4da7/32.png) [@Phaneeth](https://discourse.processing.org/u/Phaneeth)\
**Post date:** [July 9, 2018, 5:38pm UTC](https://discourse.processing.org/t/unable-to-run-motion-sensor-example-provided-by-daniel-on-pi-error-when-trying-to-use-copy/1635/1 "2018-07-09T17:38:38Z")

</div>

I am trying to run the example on motion sensor on raspberry p. I am caught with this error. The reason for this error is in prevframe.copy

Code:

> import processing.video.\*;
> 
> // Variable for capture device  
> Capture video;  
> // Previous Frame  
> PImage prevFrame;
> 
> // How different must a pixel be to be a “motion” pixel  
> float threshold = 50;
> 
> void setup() {  
> size(320, 240);  
> // Using the default capture device  
> video = new Capture(this, width, height);  
> video.start();  
> // Create an empty image the same size as the video  
> prevFrame = createImage(video.width, video.height, RGB);  
> }
> 
> // New frame available from camera  
> void captureEvent(Capture video) {  
> // Save previous frame for motion detection!!  
> prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);  
> prevFrame.updatePixels();  
> video.read();  
> }
> 
> void draw() {  
> background(0);
> 
> // You don’t need to display it to analyze it!  
> image(video, 0, 0);
> 
> video.loadPixels();  
> prevFrame.loadPixels();
> 
> // Begin loop to walk through every pixel  
> // Start with a total of 0  
> float totalMotion = 0;
> 
> // Sum the brightness of each pixel  
> for (int i = 0; i \< video.pixels.length; i ++ ) {  
> // Step 2, what is the current color  
> color current = video.pixels[i];
> 
> ```
> // Step 3, what is the previous color
> color previous = prevFrame.pixels[i];
> 
> // Step 4, compare colors (previous vs. current)
> float r1 = red(current); 
> float g1 = green(current);
> float b1 = blue(current);
> float r2 = red(previous); 
> float g2 = green(previous);
> float b2 = blue(previous);
> 
> // Motion for an individual pixel is the difference between the previous color and current color.
> float diff = dist(r1, g1, b1, r2, g2, b2);
> // totalMotion is the sum of all color differences. 
> totalMotion += diff;
> 
> ```
> 
> }
> 
> // averageMotion is total motion divided by the number of pixels analyzed.  
> float avgMotion = totalMotion / video.pixels.length;
> 
> // Draw a circle based on average motion  
> noStroke();  
> fill(0);  
> float r = avgMotion \* 2;  
> ellipse(width/2, height/2, r, r);  
> }

[Link for Motion Sensor code](http://learningprocessing.com/examples/chp16/example-16-14-MotionSensor)

error, disabling captureEvent()  
java.lang.reflect.InvocationTargetException  
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
at java.lang.reflect.Method.invoke(Method.java:498)  
at processing.video.Capture.fireCaptureEvent(Unknown Source)  
at processing.video.Capture.invokeEvent(Unknown Source)  
at processing.video.Capture$3.rgbFrame(Unknown Source)  
at org.gstreamer.elements.RGBDataAppSink$AppSinkNewBufferListener.newBuffer(RGBDataAppSink.java:162)  
at org.gstreamer.elements.AppSink$2.callback(AppSink.java:184)  
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)  
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
at java.lang.reflect.Method.invoke(Method.java:498)  
at com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback(CallbackReference.java:485)  
at com.sun.jna.CallbackReference$DefaultCallbackProxy.callback(CallbackReference.java:515)  
Caused by: java.lang.NullPointerException  
at sketch\_motionsensing\_ae.captureEvent(sketch\_motionsensing\_ae.java:59)  
… 15 more

---

<div class="post-metadata">

**Author:** ![gohai](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gohai/32/25_2.png) [@gohai](https://discourse.processing.org/u/gohai)\
**Post date:** [July 10, 2018, 3:47pm UTC](https://discourse.processing.org/t/unable-to-run-motion-sensor-example-provided-by-daniel-on-pi-error-when-trying-to-use-copy/1635/2 "2018-07-10T15:47:10Z")

</div>

@Phaneeth What is the version of Processing’s video library that you have installed on the Raspberry Pi?
